In this article I will explain with an example, how to implement CheckChanged event of CheckBox inside GridView in ASP.Net using C# and VB.Net.
When a CheckBox is checked or unchecked, a loop will be executed over the GridView rows and all the rows whose CheckBox is checked will be marked as selected and will be copied to another GridView.
The data from the selected GridView Cells or controls like Label, TextBox, DropDownList, etc. will be fetched and copied to the other GridView.
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. The CheckBox has been assigned an OnCheckedChanged event handler and the AutoPostBack property is set to True.
Below the GridView, there’s another GridView control.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkRow" runat="server" OnCheckedChanged="CheckBox_Changed" AutoPostBack="true" />
</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 />
<br />
<u>Selected Rows</u>
<br />
<br />
<asp:GridView ID="gvSelected" runat="server" 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[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
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(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Implementing CheckChanged event of CheckBox in GridView
When the CheckBox is checked or unchecked, 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 CheckBox_Changed(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 CheckBox_Changed(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
Screenshot
Demo
Downloads