Tuesday, 28 January 2014

GridView with CheckBox: Get Selected Rows in ASP.Net

HTML Markup
The HTML Markup consists of an ASP.Net GridView with CheckBox in the TemplateField column of GridView. There’s a Button that will fetch the selected rows of GridView and will display the same in another GridView below it.
<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 namespaces.
C#
using System.Data;
 
VB.Net
Imports System.Data
 
 
Binding the GridView
I have made use of DataTable with some dummy values for this article.
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 Records from GridView
On the click of the Button the following event handler is executed. A loop is executed over the GridView Data Rows and CheckBox is referenced. If the CheckBox is checked then the name and country are fetched from the BoundField and the TemplateField Label respectively.
The selected rows are then added to a DataTable which is then bound to 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
 
GridView with CheckBox: Get Selected Rows in ASP.Net

GridView with CheckBox: Get Selected Rows in ASP.Net

No comments:

Post a Comment