In this article I will explain with an example, how to use multiple DataKeyNames (DataKeys) property of GridView in ASP.Net using C# and VB.Net.
 
 
HTML Markup
The following HTML Markup consists of:
GridView – For displaying the records.
The GridView has been assigned a DataKeyNames property with multiple values i.e. Id and Group.
The GridView consists of two BoundField columns and one TemplateField column consisting of a Button control.
The Button has been assigned with OnClick event handler.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="Id, Group">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:ButtonID="btnGet" Text="Get Value" runat="server" OnClick="OnGet" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
 
 
Namespaces
You will need to import the following namespace.
C#
using System.Data;
 
VB.Net
Imports System.Data
 
 
Binding the GridView
Inside the Page Load event handler, the GridView is populated with data by making use of a dynamic DataTable with some records.
Note: For more details on how to use dynamic DataTable, please refer my article Dynamically create DataTable and bind to GridView in ASP.Net.
 
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 values of multiple DataKeys for a particular GridView Row
When Get Value Button is clicked, following event handler is executed.
Inside this event handler, first the GridView Row is determined using the NamingContainer property and then, the Row Index is determined.
Next, using the Row Index, the DataKeys array is accessed and the value of the Id field is referenced from 0th index, while the value of the Group field is referenced from the 1st index.
Finally, the values are displayed using JavaScript Alert Message Box.
C#
protected void OnGet(object sender, EventArgs e)
{
    //Determine the RowIndex of the Row whose Button was clicked.
    int rowIndex = ((sender as Button).NamingContainer as GridViewRow).RowIndex;
 
    //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();
 
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Id: " + id + "\\nGroup: " + group + "');", true);
}
 
VB.Net
Protected Sub OnGet(sender As Object, e As EventArgs)
    'Determine the RowIndex of the Row whose Button was clicked.
    Dim rowIndex As Integer = TryCast(TryCast(sender, Button).NamingContainer, GridViewRow).RowIndex
 
    '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()
 
    ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Id: " & id & "\nGroup: " & group & "');", True)
End Sub
 
 
Screenshot
Using Multiple DataKeyNames (DataKeys) in ASP.Net GridView with examples
 
 
Demo
 
 
Downloads