I realize this thread is almost 2 years old, but I had the same question today. I searched extensively and never found an answer. When I searched for "gridview rowdatabound datakey" on Google, this is the first link given. I finally figured out how to do it, so I thought I'd post what I figured out here in case somebody else is having the same problem I was.
The task I was trying to perform was to fill another gridview in each row based on the datakey of that row. To use the datakey for something else, you'll have to change the code a little. Also, I am a C# guy. I used a converter (
http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx) to get the vb code, so I'm not positive the syntax is correct, but it should be pretty close. When I use the same converter to go from VB to C#, it usually works.
The code below works when there is one datakey in the GridView. I didn't test to see what would happen if more datakeys were set in the gridview. I also didn't figure out how to get other datakeys if there are more than one. This is for if you have one datakey, and you want to get the datakey of the current row during rowdatabound:
VB:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
DirectCast(e.Row.FindControl("SqlDataSource1"), SqlDataSource).SelectParameters("primaryKey").DefaultValue = DirectCast(sender, GridView).DataKeys(e.Row.RowIndex).Value.ToString()
End If
End Sub
C#:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((SqlDataSource)e.Row.FindControl("SqlDataSource1")).SelectParameters["primaryKey"].DefaultValue = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
}
}