In this article I will explain with an example, how to check whether CheckBox in GridView is checked or not in ASP.Net using C# and VB.Net.
When the Button is clicked, a loop will be executed over GridView Rows and then it will be determined whether the CheckBox inside the GridView Row is checked or not in ASP.Net.
Related Articles on GridView and CheckBoxes:
HTML Markup
The HTML Markup consists of an ASP.Net GridView containing two TemplateField columns and a BoundField column.
The TemplateField columns consist of a CheckBox and a Label respectively.
Below the GridView, there’s a Button and another GridView control.
<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkRow" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:TemplateField HeaderText="Country" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnGetSelected" runat="server" Text="Get selected records" OnClick="GetSelectedRecords" />
<hr />
<u>Selected Rows</u>
<br />
<asp:GridView ID="gvSelected" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
Namespaces
You will need to import the following namespace.
C#
VB.Net
Binding the ASP.Net GridView control
The GridView is populated with a dynamic DataTable with some dummy data inside the Page Load event.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Name"), new DataColumn("Country") });
dt.Rows.Add("John Hammond", "Canada");
dt.Rows.Add("Rick Stewards", "United States");
dt.Rows.Add("Huang He", "China");
dt.Rows.Add("Mudassar Khan", "India");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Name"), New DataColumn("Country")})
dt.Rows.Add("John Hammond", "Canada")
dt.Rows.Add("Rick Stewards", "United States")
dt.Rows.Add("Huang He", "China")
dt.Rows.Add("Mudassar Khan", "India")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Checking whether CheckBox in GridView is checked or not in ASP.Net
When the Button is clicked, a loop is executed over the GridView Rows and CheckBox control is referenced. If the CheckBox is checked (selected), the name and country values are fetched from the BoundField and the TemplateField columns and are added to a DataTable.
Finally, the DataTable is used to populate the other GridView which displays the selected records.
C#
protected void GetSelectedRecords(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Name"), new DataColumn("Country") });
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
if (chkRow.Checked)
{
string name = row.Cells[1].Text;
string country = (row.Cells[2].FindControl("lblCountry") as Label).Text;
dt.Rows.Add(name, country);
}
}
}
gvSelected.DataSource = dt;
gvSelected.DataBind();
}
VB.Net
Protected Sub GetSelectedRecords(sender As Object, e As EventArgs)
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Name"), New DataColumn("Country")})
For Each row As GridViewRow In GridView1.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim chkRow As CheckBox = TryCast(row.Cells(0).FindControl("chkRow"), CheckBox)
If chkRow.Checked Then
Dim name As String = row.Cells(1).Text
Dim country As String = TryCast(row.Cells(2).FindControl("lblCountry"), Label).Text
dt.Rows.Add(name, country)
End If
End If
Next
gvSelected.DataSource = dt
gvSelected.DataBind()
End Sub
Screenshots
GridView control with CheckBoxes
Selected (checked) rows copied to the other GridView
Demo
Downloads