In this article I will explain with some examples, how to get value of DataKeyNames (DataKeys) in RowCommand and RowDataBound events of ASP.Net GridView using C# and VB.Net.
 
What are DataKeyNames and DataKeys?
To get detailed explanation about DataKeyNames and DataKeys, please refer my article Understanding ASP.Net GridView DataKeyNames (DataKeys).
 
HTML Markup
For the below GridView I have set multiple DataKeyNames i.e. Id and Group. Both the DataKey values will be fetched on Button click.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="Id, Group"
OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand">
<Columns>
    <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
    <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="100" />
    <asp:ButtonField CommandName="Select" Text="Select" ButtonType="Button" />
</Columns>
</asp:GridView>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
 
VB.Net
Imports System.Data
 
 
Binding the GridView
The GridView is populated using some dummy records using DataTable.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Id"), new DataColumn("Group"), new DataColumn("Name"), new DataColumn("Country") });
        dt.Rows.Add(1, "A", "John Hammond", "United States");
        dt.Rows.Add(2, "B", "Mudassar Khan", "India");
        dt.Rows.Add(3, "A", "Suzanne Mathews", "France");
        dt.Rows.Add(4, "B", "Robert Schidner", "Russia");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As New DataTable()
        dt.Columns.AddRange(New DataColumn(3) {New DataColumn("Id"), New DataColumn("Group"), New DataColumn("Name"), New DataColumn("Country")})
        dt.Rows.Add(1, "A", "John Hammond", "United States")
        dt.Rows.Add(2, "B", "Mudassar Khan", "India")
        dt.Rows.Add(3, "A", "Suzanne Mathews", "France")
        dt.Rows.Add(4, "B", "Robert Schidner", "Russia")
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub
 
 
Getting the value of DataKeyNames (DataKeys) in RowDataBound event
The row index can be easily determined using the Row.RowIndex property of GridViewRowEventArgs object and using the row index, the DataKey values can be easily fetched as shown below.
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //Get the value of column from the DataKeys using the RowIndex.
        int id = Convert.ToInt32(GridView1.DataKeys[e.Row.RowIndex].Values[0]);
        string group = GridView1.DataKeys[e.Row.RowIndex].Values[1].ToString();
    }
}
 
VB.Net
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        'Get the value of column from the DataKeys using the RowIndex.
        Dim id As Integer = Convert.ToInt32(GridView1.DataKeys(e.Row.RowIndex).Values(0))
        Dim group As String = GridView1.DataKeys(e.Row.RowIndex).Values(1).ToString()
    End If
End Sub
 
Get value of DataKeyNames (DataKeys) in RowCommand and RowDataBound events of ASP.Net GridView
 
 
Getting the value of DataKeyNames (DataKeys) in RowCommand event
The row index can be easily determined using the CommandArgument property of GridViewCommandEventArgs object and using the row index, the DataKey values can be easily fetched as shown below.
C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    //Determine the RowIndex of the Row whose Button was clicked.
    int rowIndex = Convert.ToInt32(e.CommandArgument);
 
    //Get the value of column from the DataKeys using the RowIndex.
    int id = Convert.ToInt32(GridView1.DataKeys[rowIndex].Values[0]);
    string group = GridView1.DataKeys[rowIndex].Values[1].ToString();
}
 
VB.Net
Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
    'Determine the RowIndex of the Row whose Button was clicked.
    Dim rowIndex As Integer = Convert.ToInt32(e.CommandArgument)
 
    'Get the value of column from the DataKeys using the RowIndex.
    Dim id As Integer = Convert.ToInt32(GridView1.DataKeys(rowIndex).Values(0))
    Dim group As String = GridView1.DataKeys(rowIndex).Values(1).ToString()
End Sub
 
Get value of DataKeyNames (DataKeys) in RowCommand and RowDataBound events of ASP.Net GridView
 
 
Demo
 
 
Downloads