How to Get Read DataKeyNames value of GridView Row using Asp.Net C# VB.Net

When we need to bind data in a GridView column then we need to add an unique reference number for each GridView row which will help us to identify a specific object or a datarow. Some developers use hidden field to store the ID column of a table on which he can INSERT UPDATE or DELETE based on hidden ID. But this is not a good practice. Because Gridview control gives us a property named DataKeyNames on which we can assign our unique ID or code value to distinguish individual GridView Row. Here in this example i will show how one can read DataKeyNames value from code behind using C# or VB.Net. Asp.net Gridview control also provides us a facility to assign more than one ID or Code as DataKeyNames for each DataRow item. Specialy for composite primary keys.In one of my previous article i have also discuss on “Jquery to read DatakeyNames value of Selected Rows of GridView“.

Objective/Outcome:
Get_Datakeyname_gridview_csharp_VB.Net

To prepare the Example Add a GridView Like Below:

<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" HorizontalAlign="Left" AutoGenerateColumns="false" HeaderStyle-BackColor="CornflowerBlue" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White" CellPadding="5">
        <Columns>
            <asp:BoundField datafield="Name" headertext="Super Player" />
        </Columns>
    </asp:GridView>
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <asp:Label runat="server" ID="lbl" Font-Bold="true"></asp:Label>

Now Read DataKeyNames from Code Behind:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dtPlayers = new DataTable("Super_Player");
            dtPlayers.Columns.Add(new DataColumn("ID", System.Type.GetType("System.UInt64")));
            dtPlayers.Columns.Add(new DataColumn("Name"));
            dtPlayers.Rows.Add(1, "Leonel Messi");
            dtPlayers.Rows.Add(2, "Christiano Ronaldo");
            dtPlayers.Rows.Add(3, "Carlos Tevez");
            dtPlayers.Rows.Add(4, "Xavi");
            dtPlayers.Rows.Add(5, "Iniesta");
            GridView1.DataSource = dtPlayers;
            GridView1.DataBind();

            foreach (GridViewRow oRow in GridView1.Rows)
                lbl.Text+=("DataKeyNames="+GridView1.DataKeys[oRow.RowIndex].Value)+"<br/>";
        }
    }

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim dtPlayers As DataTable = New DataTable("Super_Player")
            dtPlayers.Columns.Add(New DataColumn("ID", System.Type.GetType("System.UInt64")))
            dtPlayers.Columns.Add(New DataColumn("Name"))
            dtPlayers.Rows.Add(1, "Leonel Messi")
            dtPlayers.Rows.Add(2, "Christiano Ronaldo")
            dtPlayers.Rows.Add(3, "Carlos Tevez")
            dtPlayers.Rows.Add(4, "Xavi")
            dtPlayers.Rows.Add(5, "Iniesta")
            GridView1.DataSource = dtPlayers
            GridView1.DataBind()

            For Each oRow As GridViewRow In GridView1.Rows
                lbl.Text += "DataKeyNames=" + GridView1.DataKeys(oRow.RowIndex).Value.ToString() + "<br/>"
            Next
        End If
    End Sub

If you can identify a single or unique row of a GridView from code behind then you can do anything as you want.

For practice find full code sample from below:

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, Gridview, VB.Net

Leave a Reply

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

AlphaOmega Captcha Classica  –  Enter Security Code
     
 

*