Get multiple datakeyname values from gridview rowcommand in asp.net

Thank you for reading this post, don't forget to subscribe!

DataKeyNames=”yourColumnName”
If we want to get this DataKeyNames value from gridview in rowcommand event we will get it like this

protected void gvUserInfo_RowDataCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow gvrow;
string str = gvUserInfo.DataKeys[gvrow.RowIndex].Value.ToString();
}
If we want to declare multiple DataKeyNames values we will declare it as

DataKeyNames=”Column1,Column2,Column3″
Now if we want get these DataKeyNames values we will declare it like this

protected void gvUserInfo_RowDataCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow gvrow;
string value1 = gvUserInfo.DataKeys[gvrow.RowIndex].Values[0].ToString();
string value2 = gvUserInfo.DataKeys[gvrow.RowIndex].Values[1].ToString();
string value3 = gvUserInfo.DataKeys[gvrow.RowIndex].Values[2].ToString();
}
We can get these datakeynames values with datakeyname also that would be like this

protected void gvUserInfo_RowDataCommand(object sender, GridViewCommandEventArgs e)
{
string value1 = gvUserInfo.DataKeys[gvrow.RowIndex].Values[“Column1”].ToString();
string value2 = gvUserInfo.DataKeys[gvrow.RowIndex].Values[“Column2”].ToString();
string value3 = gvUserInfo.DataKeys[gvrow.RowIndex].Values[“Column3″].ToString();
}
If we want to get DataKeyNames values with foreach statement in rowcommand event that would be like this

protected void gvUserInfo_RowDataCommand(object sender, GridViewCommandEventArgs e)
{
foreach (GridViewRow gvrow in gvUserInfo.Rows)
{
string value1 = gvUserInfo.DataKeys[gvrow.RowIndex].Values[0].ToString();
string value2 = gvUserInfo.DataKeys[gvrow.RowIndex].Values[1].ToString();
string value3 = gvUserInfo.DataKeys[gvrow.RowIndex].Values[2].ToString();
}
}
For sample check below code
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head id=”Head1″ runat=”server”>
<title>Get Gridview datakeyname values in rowcommand event in asp.net</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:GridView ID=”gvUserInfo” runat=”server” AutoGenerateColumns=”false” DataKeyNames=”MobileId,MobileName” OnRowCommand=”gvUserInfo_RowDataCommand” >
<HeaderStyle BackColor=”#df5015″ Font-Bold=”true” ForeColor=”White” />
<Columns>
<asp:BoundField DataField=”MobileId” HeaderText=”MobileId” />
<asp:BoundField DataField=”MobileName” HeaderText=”MobileName” />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
After completion of aspx page add following namespaces in codebehind

C# Code———————————————————————————————
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
After that add following code in code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = new SqlConnection(“Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB”);
con.Open();
SqlCommand cmd = new SqlCommand(“select * from MobileDetails order by Priority asc”, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
gvUserInfo.DataSource = ds;
gvUserInfo.DataBind();
}
}
protected void gvUserInfo_RowDataCommand(object sender, GridViewCommandEventArgs e)
{
foreach (GridViewRow gvrow in gvUserInfo.Rows)
{
string mobileId = gvUserInfo.DataKeys[gvrow.RowIndex].Values[0].ToString();
string mobileName = gvUserInfo.DataKeys[gvrow.RowIndex].Values[1].ToString();
}
}
}