Updated on Kisan Patel
This tutorial will explain you how to select multiple rows from GridView and retrieve selected records in ASP.NET.
Default.aspx
<asp:GridView ID="GridView1" OnSorting="SortRecords" runat="server" AllowSorting="True" DataKeyNames="Id" AllowPaging="True" OnPageIndexChanging="PaginateGridView" PageSize="5" PagerSettings-Mode="Numeric" EnablePersistedSelection="True" AutoGenerateColumns="False"> <Columns> <asp:TemplateField HeaderText="Select"> <ItemTemplate> <asp:CheckBox ID="chkSelect" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Id" SortExpression="Id"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("Id") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="FirstName" SortExpression="FirstName"> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="LastName" SortExpression="LastName"> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Bind("LastName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Email" SortExpression="Email"> <ItemTemplate> <asp:Label ID="Label4" runat="server" Text='<%# Bind("Email") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="IsActive" SortExpression="IsActive"> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("IsActive") %>' Enabled="false" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:Button ID="btnFind" runat="server" Text="Get Selected Records" OnClick="GetSelectedRecords" />
In the above code, on the .aspx page we have a GridView with asp:TemplateField
.
Default.aspx.cs
using System; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Web.UI.WebControls; namespace GridViewDemo { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindData(); } } private DataTable GetData() { DataTable table = new DataTable(); // get the connection using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString)) { // write the sql statement to execute string sql = "SELECT Id, FirstName, LastName, Email, IsActive FROM Students"; // instantiate the command object to fire using (SqlCommand cmd = new SqlCommand(sql, conn)) { // get the adapter object and attach the command object to it using (SqlDataAdapter sdt = new SqlDataAdapter(cmd)) { // fire Fill method to fetch the data and fill into DataTable sdt.Fill(table); } } } return table; } private void BindData() { // specify the data source for the GridView GridView1.DataSource = GetData(); // bind the data now GridView1.DataBind(); } protected void PaginateGridView(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; this.BindData(); } protected void SortRecords(object sender, GridViewSortEventArgs e) { string sortExpression = e.SortExpression; string direction = string.Empty; if (SortDirection == SortDirection.Ascending) { SortDirection = SortDirection.Descending; direction = " DESC"; } else { SortDirection = SortDirection.Ascending; direction = " ASC"; } DataTable table = GetData(); table.DefaultView.Sort = sortExpression + direction; GridView1.DataSource = table; GridView1.DataBind(); } /// <summary> /// Gets or sets the grid view sort direction. /// </summary> /// <value> /// The grid view sort direction. /// </value> public SortDirection SortDirection { get { if (ViewState["SortDirection"] == null) { ViewState["SortDirection"] = SortDirection.Ascending; } return (SortDirection)ViewState["SortDirection"]; } set { ViewState["SortDirection"] = value; } } protected void GetSelectedRecords(object sender, EventArgs e) { Response.Write("<h3>Selected records</h3>"); foreach (GridViewRow row in GridView1.Rows) { CheckBox chk = (CheckBox)row.FindControl("chkSelect"); if (chk.Checked) { // get the selected Id and manipulate data Response.Write("Id: " + GridView1.DataKeys[row.RowIndex].Value + "<br />"); } } } } }
The button fires GetSelectedRecords server side events, in which we have looped through all the rows of the GridView and found the CheckBox. In case the checkbox is checked by the user, we have retrieved the DataKey
value of that row.
Demo