Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loop through gridview and get data key value

Tags:

asp.net

vb.net

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
like image 702
JDV590 Avatar asked Nov 25 '11 03:11

JDV590


2 Answers

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
}
like image 70
AnandMohanAwasthi Avatar answered Oct 11 '22 13:10

AnandMohanAwasthi


You have to use Value property.

Dim theid As Integer = Integer.Parse(GridView1.DataKeys(therowindex).Value.ToString())
like image 27
KV Prajapati Avatar answered Oct 11 '22 15:10

KV Prajapati