In this article I will explain with an example, how to get the selected rows of GridView using CheckBox in ASP.Net with C# and VB.Net.
When the Button to get the selected rows of GridView is clicked, 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.
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#
using System.Data;
 
VB.Net
Imports System.Data
 
 
Binding the ASP.Net GridView control
The GridView is populated with a dynamic DataTable with some dummy data inside the Page Load event.
Note: You can learn more about this dynamic DataTable in my article Create DataTable dynamically and bind to GridView in ASP.Net.
 
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
 
 
Fetching selected (checked) rows from GridView
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
GridView with CheckBox: Get Selected Rows in ASP.Net
 
Selected (checked) rows copied to the other GridView
GridView with CheckBox: Get Selected Rows in ASP.Net
 
 
Demo
 
 
Downloads