Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data table Question.

Status
Not open for further replies.

mkohl

Programmer
Feb 22, 2005
82
US
I am experiencing a slight problem. I loaded information into a data table. But for some reason after I leave the sub routine that populated the table, everything is lost. So when I goto a button click event and try to use the information that is in the table.. it basically gone.. I get "" in all the cells. Anyone have any ideals why this would happen or any suggestions would be helpful.

'some code
dim dt2 As New DataTable

'sub that populates the table

dt.Columns.Add("Part Number", GetType(TemplateColumn))
dt.Columns.Add("TempPart", GetType(String))
dt.Columns.Add("Product Type", GetType(TemplateColumn))
dt.Columns.Add("TempType", GetType(String))
dt.Columns.Add("Price", GetType(String))
dt.Columns.Add("Description", GetType(TemplateColumn))
dt.Columns.Add("Add to cart", GetType(TemplateColumn))
dt.Columns.Add("Weight", GetType(String))

For Each row In ds.Tables(0).Rows
Dim price As String
price = ds.Tables(0).Rows(i).Item(3)
price = CType(price, Decimal)
price = FormatNumber(price, 2)
row = dt.NewRow
row.Item(0) = ""
row.Item(1) = ds.Tables(0).Rows(i).Item(4)
row.Item(2) = ""
row.Item(3) = ds.Tables(0).Rows(i).Item(1)
row.Item(4) = price
row.Item(5) = ""

dt.Rows.Add(row)
i += 1
Next


Datagrid2.DataSource = dt
Datagrid2.DataBind()


When I click on a button after binding the information to a datagrid.. I check the row count in for data table and it returns count = 0

-mike

 
When you click a button, it causes a post back. This will then instantiate your datatable everytime the button is clicked, therefore it will have no data. In your case, the data will show in the grid when the subroutine is called, but not on subsequnt post backs. In this case, I usually use a Session variable to hold the datatable between post backs.

Jim
 
Thanks for the response, I figured since it was doing a postback, the table was being cleared. Also, since I am using Template Columns I could not pull the information I needed from the datagrid. Everytime I tried to reference a cell that was a template column it would appear blank.

One thing I did to fix the problem, was to add a new bound column to the column collection for the datagrid, and set its visiable property to false. I also added a new column to the table. Even though the visiability was set to false I was still able to pull the column information I needed.

Thanks,
-mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top