Introduction:
Here I will explain how to get selected row cell value from gridview in asp.net using C# and VB.NET or get cell values from selected row cell values from gridview when checkbox selected in asp.net using C# and VB.NET.
The below code to get checkbox selected row values from gridview.
The below code to get checkbox selected row values from gridview.
foreach(GridViewRow gvrow in
gvDetails.Rows)
{
CheckBox chk = (CheckBox)gvrow.FindControl("chkSelect");
if (chk != null &
chk.Checked)
{
str +=
gvDetails.DataKeys[gvrow.RowIndex].Value.ToString() + ',';
strname += gvrow.Cells[2].Text+',';
}
}The following code in aspx page:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Get
Checkbox Selected Row Values from Gridview in Asp.net</title>
</head>
<body>
<form id="form1"
runat="server">
<div>
<asp:GridView ID="gvDetails" DataKeyNames="UserId" AutoGenerateColumns="false" CellPadding="5" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="UserId" DataField="UserId" />
<asp:BoundField HeaderText="UserName" DataField="UserName" />
<asp:BoundField HeaderText="Education" DataField="Education" />
<asp:BoundField HeaderText="Location" DataField="Location" />
</Columns>
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
<asp:Button ID="btnProcess" Text="Get Selected Records" runat="server"
Font-Bold="true" onclick="btnProcess_Click" /><br />
<asp:Label ID="lblmsg" runat="server" />
</div>
</form>
</body>
</html>The blow code is aspx.cs code behind file.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
}
{
BindGridview();
}
}
protected void
btnProcess_Click(object sender, EventArgs e)
{
string str = string.Empty;
string strname = string.Empty;
foreach(GridViewRow gvrow in
gvDetails.Rows)
{
CheckBox chk = (CheckBox)gvrow.FindControl("chkSelect");
if (chk != null &
chk.Checked)
{
str += gvDetails.DataKeys[gvrow.RowIndex].Value.ToString()
+ ',';
strname += gvrow.Cells[2].Text+',';
}
}
str= str.Trim(",".ToCharArray());
strname = strname.Trim(",".ToCharArray());
lblmsg.Text = "Selected
UserIds: " + str + "
" + "Selected UserNames: " + strname+""; }
protected void BindGridview()
{
DataTable dt = new DataTable();
dt.Columns.Add("UserId", typeof(Int32));
dt.Columns.Add("UserName", typeof(string));
dt.Columns.Add("Education", typeof(string));
dt.Columns.Add("Location", typeof(string));
dt.Rows.Add(1, "Muhammed Asad", "BCIT" , "Karachi");
dt.Rows.Add(2, "Muhammed Ali", "Msc", "Karachi");
dt.Rows.Add(3, "Madhav Sai", "MS", "Karachi");
dt.Rows.Add(4, "Asma", "MBA", "Karachi");
dt.Rows.Add(6, "Faheem Shah", "MBBS", "Karachi");
dt.Rows.Add(7, "Asif Raza", "B.Tech", "Karachi");
dt.Rows.Add(8, "Daniel", "CA", "Karachi");
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
{
DataTable dt = new DataTable();
dt.Columns.Add("UserId", typeof(Int32));
dt.Columns.Add("UserName", typeof(string));
dt.Columns.Add("Education", typeof(string));
dt.Columns.Add("Location", typeof(string));
dt.Rows.Add(1, "Muhammed Asad", "BCIT" , "Karachi");
dt.Rows.Add(2, "Muhammed Ali", "Msc", "Karachi");
dt.Rows.Add(3, "Madhav Sai", "MS", "Karachi");
dt.Rows.Add(4, "Asma", "MBA", "Karachi");
dt.Rows.Add(6, "Faheem Shah", "MBBS", "Karachi");
dt.Rows.Add(7, "Asif Raza", "B.Tech", "Karachi");
dt.Rows.Add(8, "Daniel", "CA", "Karachi");
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
Damo:
No comments:
Post a Comment