Reputation: 651
I am trying to loop through the rows of my gridview and retrieve the data key value of each row and then execute some code to run sql queries. How can I get the data key value of each row in variable? right now I am receiving an error message saying:
value of type system.web.ui.webcontrols.datakey cannot be converted to integer.
Here is my code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For Each row As GridViewRow In GridView1.Rows
Dim therowindex As Integer = row.RowIndex
Dim theid As Integer = GridView1.DataKeys([therowindex])
'execute some more code running queries using the data key value.
Next
End Sub
Upvotes: 5
Views: 28325
Reputation: 839
You can iterate through the .Rows
collection, and then find the row's DataKey
value.
foreach(GridViewRow row in GridView1.Rows)
{
string theDataKey = GridView1.DataKeys[row.RowIndex].Value.ToString();
// Do something with your index/key value
}
Upvotes: 13
Reputation: 94645
You have to use Value property.
Dim theid As Integer = Integer.Parse(GridView1.DataKeys(therowindex).Value.ToString())
Upvotes: 7