Welcome Guest Search | Active Topics | Sign In | Register

Getting a Grids DataTable for Easy Update Options
Mark J
Posted: Saturday, June 13, 2009 11:01:14 PM
Rank: Newbie
Groups: Member

Joined: 6/13/2009
Posts: 1
An easy way to update data from a grid to a database would be to get its underlying datatable and use the update method of a dataadapter. This allows for updating a database from a grid in about 5 lines of code.

The Grid has a datatable property but I don't seem to get at it.

I've created a Save button with the following code:

Dim DT as New DataTable

DT = CType(Grid1.DataSource, DataTable)

Lable1.Text = DT.Rows.Count.ToString


However this gives a "Object reference not set to an instance of an object" error on the third line.

Is there any way to get the underlying datasource of the Grid?
eo_support
Posted: Saturday, June 13, 2009 11:09:06 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

The DataSource exists during the data binding. Once data binding is done (after DataBind call returned), the Grid has retrieved all data from the DataSource into the Grid and the DataSource is no longer associated with the Grid from this point on.

After that point you will need to get data from the Grid through its Items property. For example, the following code gets item count:

Code: Visual Basic.NET
Grid1.Items.Count


The following code gets cell values on (0, 0):

Code: Visual Basic.NET
Grid1.Items(0).Cells(0).Value


Hope this helps.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.