Recovering Datagrid Data with Datakeys November 19 2004
When we bind data to a datagrid, it is always important to user the OnItemCommand and DataKeys fields. These will allow you to reference specific rows of information when a user clicks on a certain item on a certain row.
The main topic of this post is how to recover the data from that row in a reasonable manner. Suppose the following:
Datagrid = dgGrid
OnItemCommand = dgGrid_OnItemCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs) Handles dgFileSystem.ItemCommand)
datakeyfield = intID
Then when the OnItemCommand fires:
dim intMyID = dgGrid.datakeys(e.item.itemIndex)
To retrieve the rest of the values, the data set that is bound to the grid requires a primary key and the data should have been stored into a session variable. Here is an example using a DataTable as the source for dgGrid
Dim dt As DataTable = CType(Session(”dtData”), DataTable)
Dim intMyID = dgGrid.datakeys(e.item.itemIndex)
Dim dr As DataRow = dtData.Rows.Find(dgGrid.datakeys(e.item.itemIndex))
Now all the data from that row will appear within “dr”
Reference the values using dr.item() e.g. dr.item(”MY_COLUMN_NAME”)
- Posted in : .NET
- Author : site admin

Comments
Sorry comments are closed for this entry