Wednesday, 29 October 2014

Get Asp.net Gridview Selected Row Values on Button click in C#

Get Asp.net Gridview Selected Row Values on Button click in C# 

Here I will explain how to add checkboxe in asp.net gridview and get gridview selected rows data on button click in asp.net and get checked rows data on button click and how to work with those checked rows in asp.net using c#.

First create one new web application and open your Default.aspx and write the following code .

 <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Get Checkbox Selected Rows in Asp.net Gridview</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView AutoGenerateColumns="false" ID="gvDetails" CellPadding="5" runat="server">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkDetails" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UserId">
<ItemTemplate>
<asp:Label id="lblUserid" runat="server" Text='<%#Eval("UserId") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UserName">
<ItemTemplate>
<asp:Label id="lblUsername" runat="server" Text='<%#Eval("Username") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Education">
<ItemTemplate>
<asp:Label id="lblEducation" runat="server" Text='<%#Eval("Education") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<asp:Button ID="btngetdetails" Text="Get Selected Checkboxes" runat="server"
onclick="btngetdetails_Click" />
<br />
<b>Selected Values are:</b><asp:Label ID="lblselected" runat="server" />
</form>
</body>
</html>

 Now in code behind file write the code like as shown below
 using System;
using System.Data;
using System.Web.UI.WebControls;
public partial class BindGridviewwithCheckboxes : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindGridviewData();
}
}
protected void BindGridviewData()
{
        DataTable dt = new DataTable();
        dt.Columns.Add("UserId", typeof(Int32));
        dt.Columns.Add("UserName", typeof(string));
        dt.Columns.Add("Education", typeof(string));
        dt.Rows.Add(1, "Sumit Kemar Sen", "B.Tech");
        dt.Rows.Add(2, "Soumi Santra", "MA");
        dt.Rows.Add(3, "Sujoy Santra", "MCA");
        dt.Rows.Add(4, "Pradipta Chowdhury", "MCA");
        dt.Rows.Add(6, "Sourav Mukherjee", "B.Tech");
        dt.Rows.Add(7, "Sandipan Maji", "B.Tech");
        dt.Rows.Add(8, "Mithun Patra", "MCA");
        gvDetails.DataSource = dt;
        gvDetails.DataBind();
}
protected void btngetdetails_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
string str = "";
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("UserId"), new DataColumn("Status") });
foreach (GridViewRow row in gvDetails.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox bf = (row.Cells[3].FindControl("chkDetails") as CheckBox);
string id = (row.Cells[0].FindControl("lblUserid") as Label).Text;
if (bf.Checked)
{
str = str+ id + ",";
}
}
}
str = str.Remove(str.Length - 1, 1);
lblselected.Text = str;
}
}

No comments:

Post a Comment