Introduction: 
 
 
 
 
 
 
 
 
 
 
In
this article I will explain how to declare multiple datakeynames in gridview and
how to get multiple datakey values in gridview rowcommand event in asp.net. 
Description: 
In previous posts I explained many articles relating to gridview now I will explain how to declare multiple datakeyname values in gridview and how to get datakeyname values from gridview in gridview rowcommand event in asp.net.
In previous posts I explained many articles relating to gridview now I will explain how to declare multiple datakeyname values in gridview and how to get datakeyname values from gridview in gridview rowcommand event in asp.net.
Generally in
gridview we will declare DataKeyNames values like this 
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(); 
} 
} 
} 
 | 
 
VB.NET Code
Imports System 
Imports System.Data 
Imports System.Data.SqlClient 
Imports System.Web.UI.WebControls 
Partial Public Class _Default 
Inherits System.Web.UI.Page 
Protected Sub
  Page_Load(ByVal sender As Object, ByVal e As
  EventArgs) 
If Not IsPostBack Then 
Dim con As New SqlConnection("Data
  Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB") 
con.Open() 
Dim cmd As New SqlCommand("select
  * from MobileDetails order by Priority asc", con) 
Dim da As New SqlDataAdapter(cmd) 
Dim ds As New DataSet() 
da.Fill(ds) 
con.Close() 
gvUserInfo.DataSource = ds 
gvUserInfo.DataBind() 
End If 
End Sub 
Protected Sub
  gvUserInfo_RowDataCommand(ByVal sender As Object, ByVal e As
  GridViewCommandEventArgs) 
For Each gvrow As GridViewRow In
  gvUserInfo.Rows 
Dim mobileId As String = gvUserInfo.DataKeys(gvrow.RowIndex).Values(0).ToString() 
Dim mobileName As String =
  gvUserInfo.DataKeys(gvrow.RowIndex).Values(1).ToString() 
Next 
End Sub 
End Class 
 | 
 
| 
 If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.  | 
|||
 
                                        Subscribe by RSS
                                      
 
                                        Subscribe by Email
                                      
 | 
|||
                                        Subscribe by RSS
                                      
                                        Subscribe by Email
                                      
14 comments :
nice artilce..really helpful and needful
super super suresh,,, god bless u..........
I am highly impress by your Articles
But you using () bracket instead of [] ???
nice artical
nice article
Nice article . we would expect more explanation on the code.
the use of unassigned local variable gvrow error....
help!
super suresh ..
i am shiv from guntur
I am your fan/
Any info on "error: CS0165: Use of unassigned local variable 'gvrow'" ?
Nice....
Thought of sharing this:
In the RowDataCommand there is no need to declare GridViewRow gvrow.
instead of : string value1 = gvUserInfo.DataKeys[gvrow.RowIndex].Values[0].ToString();
we can do this: string value1 = gvUserInfo.DataKeys[e.RowIndex].Values[0].ToString();
Good example..
but i want to add row between all bind data just like below data
-----------------------------------
1 | ABC | TEST |
2 | XYZ | TEST |
------------------------------------
------------------------------------
3 | MNP | TEST |
------------------------------------
with editable row....please help me
Note: Only a member of this blog may post a comment.