In this article I will explain with an example, how to get value of DataKeyNames (DataKeys) in RowCommand and RowDataBound events of ASP.Net GridView using C# and VB.Net.
HTML Markup
The following HTML Markup consists of:
GridView – For displaying records.
The GridView consists of two BoundField columns and one ButtonField column.
Properties
DataKeyNames – For storing the Unique key values such as Primary Key, ID fields, etc.
Here, DataKeyNames property is set with multiple values i.e. Id and Group.
Events
GridView has been assigned with the following event handler i.e. OnRowDataBound, OnRowCommand.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="Id, Group"
OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:ButtonField CommandName="Select" Text="Select" ButtonType="Button" />
</Columns>
</asp:GridView>
Namespaces
You will need to import the following namespace.
C#
VB.Net
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.
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 inside RowDataBound Event
Inside the RowDataBound event handler, 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.
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
Getting the values of multiple DataKeys inside RowCommand Event
When Select Button is clicked, the Row Index is determined using the CommandArgument property of GridViewCommandEventArgs class object.
Then, 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 GridView1_RowDataBound(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();
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Id: " + id + "\\nGroup: " + group + "');", true);
}
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()
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Id: " & id & "\nGroup: " & group & "');", True)
End Sub
Screenshots
Getting the values of multiple DataKeys inside RowDataBound event
Getting the values of multiple DataKeys inside RowCommand event
The Form
Demo
Downloads