Loop through gridview and get data key value

I am trying to skip the rows of my gridview and get 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 a variable? I am getting an error right now:

a value of type system.web.ui.webcontrols.datakey cannot be converted to an 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 
+4
source share
2 answers

You must use the Value property.

 Dim theid As Integer = Integer.Parse(GridView1.DataKeys(therowindex).Value.ToString()) 
+6
source

You can iterate through the .Rows collection, and then find the value of the DataKey string.

 foreach(GridViewRow row in GridView1.Rows) { string theDataKey = GridView1.DataKeys[row.RowIndex].Value.ToString(); // Do something with your index/key value } 
+10
source

Source: https://habr.com/ru/post/1382993/


All Articles