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.
DataKeyNames="yourColumnName"
|
protected void
gvUserInfo_RowDataCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow gvrow;
string str =
gvUserInfo.DataKeys[gvrow.RowIndex].Value.ToString();
}
|
DataKeyNames="Column1,Column2,Column3"
|
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();
}
|
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();
}
|
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();
}
}
|
<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>
|
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
|
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();
}
}
}
|
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
10 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/