How to Get DataKeyNames from Asp.Net Gridview selected Row in C# VB.Net

Sometimes we need to work on Asp.Net GridView Selected Row. When you click on Select link then SelectedIndexChanged method will fire. Within this method to uniquely identify a record we need to get the assigned DataKeyNames. Which will be a primary key of an object or entity. After getting the key value we can do anything depends on client requirement. So getting the DataKeyNames from Asp.Net GridView selected Row is a crucial operation. Here in this article I will explain how we can do this.

Objective/Outcome:
How to get DataKeyNames from Asp.Net Gridview selected Row in C# VB.Net

First design the HTML like below:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
    DataKeyNames="ID" AutoGenerateSelectButton="True"  
    HeaderStyle-BackColor="CornflowerBlue" HeaderStyle-Font-Bold="true" 
    HeaderStyle-ForeColor="White" CellPadding="5" 
    onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
    <asp:BoundField DataField="Name" HeaderText="Name">
    </asp:BoundField>
    <asp:BoundField DataField="Address" HeaderText="Address">
    </asp:BoundField>
    <asp:BoundField DataField="Contact" HeaderText="Contact no">
    </asp:BoundField>
</Columns>
</asp:GridView>
<br />
<hr />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
</asp:Content>

Namespace to include:
1. System.Data

Bind the GridView:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dtSupplier = new DataTable("Supplier");
            dtSupplier.Columns.Add(new DataColumn("ID"));
            dtSupplier.Columns.Add(new DataColumn("Name"));
            dtSupplier.Columns.Add(new DataColumn("Address"));
            dtSupplier.Columns.Add(new DataColumn("Contact"));
            dtSupplier.Rows.Add("st0001", "S.R. Steel", "Uttara, Dhaka", "01711xxxxxx");
            dtSupplier.Rows.Add("ir0039", "Shadesh builders", "Rampura, Dhaka", "01711yyyyyy");
            dtSupplier.Rows.Add("cr0042", "Orchard confec.", "Shahabag, Dhaka", "01711zzzzzz");
            dtSupplier.Rows.Add("er0078", "Windblow", "Mirpur, Dhaka", "01711qqqqqq");
            dtSupplier.Rows.Add("bd0301", "Rahimkarim", "Badda, Dhaka", "01711oooooo");

            GridView1.DataSource = dtSupplier;
            GridView1.DataBind();
        }
    }

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim dtSupplier As New DataTable("Supplier")
            dtSupplier.Columns.Add(New DataColumn("ID"))
            dtSupplier.Columns.Add(New DataColumn("Name"))
            dtSupplier.Columns.Add(New DataColumn("Address"))
            dtSupplier.Columns.Add(New DataColumn("Contact"))
            dtSupplier.Rows.Add("st0001", "S.R. Steel", "Uttara, Dhaka", "01711xxxxxx")
            dtSupplier.Rows.Add("ir0039", "Shadesh builders", "Rampura, Dhaka", "01711yyyyyy")
            dtSupplier.Rows.Add("cr0042", "Orchard confec.", "Shahabag, Dhaka", "01711zzzzzz")
            dtSupplier.Rows.Add("er0078", "Windblow", "Mirpur, Dhaka", "01711qqqqqq")
            dtSupplier.Rows.Add("bd0301", "Rahimkarim", "Badda, Dhaka", "01711oooooo")

            GridView1.DataSource = dtSupplier
            GridView1.DataBind()
        End If
    End Sub

Now Get DataKeyNames from Asp.Net Gridview Selected Row:
C# Code:

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Reading DataKeyName
        Label1.Text = "DataKeyName: " + GridView1.DataKeys[GridView1.SelectedRow.RowIndex].Value.ToString();
    }

VB.Net Code:

    Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs)
        ' Reading DataKeyName
        Label1.Text = "DataKeyName: " & GridView1.DataKeys(GridView1.SelectedRow.RowIndex).Value.ToString()
    End Sub

Hope now we can get selected DataKeyNames from GridView Selected Row in C# & VB.Net.

Download Code Example C#        Download Code Example VB.Net

One thought on “How to Get DataKeyNames from Asp.Net Gridview selected Row in C# VB.Net

  • March 28, 2015 at 6:48 pm
    Permalink

    Very nice tutorial, helped me a lot. Keep it up.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code