asp.net Gridview checkbox

 
We have added the CheckBox into Gridview Header template with autopostback enabled and onChecked event of it
we are checking all the checkboxs in gridview

Here is Gridview code

 

 

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField HeaderText="ID">
                    <ItemTemplate>
                        <asp:Label ID="lblID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"ID") %>'></asp:Label>
                    </ItemTemplate>
                  
                </asp:TemplateField>
                <asp:TemplateField HeaderText="First Name">
                    <ItemTemplate>
                        <asp:Label ID="lblfirstName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"FirstName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Last Name">
                    <ItemTemplate>
                        <asp:Label ID="lblLastName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"LastName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Email">
                    <ItemTemplate>
                        <asp:Label ID="lblEmail" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Email") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Check all">
                    <HeaderTemplate>
                        <asp:CheckBox ID="CheckBox2" AutoPostBack="true" runat="server"  OnCheckedChanged="CheckBox2_CheckedChanged1" />
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1"  runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>


We have added the CheckBox into Gridview Header template with autopostback enabled and onChecked event of it
we are checking all the checkboxs in gridview

C#.net code behind

  protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

GridView1.DataSource = GridDataProvider.GetData();

GridView1.DataBind();

}

}

 

 

protected void CheckBox2_CheckedChanged1(object sender, EventArgs e)

{

foreach (GridViewRow r in GridView1.Rows)

{

((CheckBox)r.FindControl("CheckBox1")).Checked = true;

}

}

Vb.Net code 

Protected Sub CheckBox2_CheckedChanged1(ByVal sender As Object, ByVal e As EventArgs)
    For Each r As GridViewRow In GridView1.Rows
        DirectCast(r.FindControl("CheckBox1"), CheckBox).Checked = True
       
    Next
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Not Page.IsPostBack Then
        GridView1.DataSource = GridDataProvider.GetData()
        GridView1.DataBind()
    End If
End Sub

 
The GridView Data provider class will provide data to Grid

  using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
/// <summary>
/// Summary description for GridDataProvider
/// </summary>
public class GridDataProvider
{
 public GridDataProvider()
 {
  
 }

    public static DataTable GetData()
    {
       
        DataTable dt = new DataTable();

        dt.Columns.Add( new DataColumn("ID",typeof(int)));
        dt.Columns.Add(new DataColumn("FirstName",typeof(string)));
        dt.Columns.Add(new DataColumn("LastName",typeof(string)));
        dt.Columns.Add(new DataColumn("Email",typeof(string)));


        DataRow dr = dt.NewRow();
        dr[0] = 1;
        dr[1] = "Mike";
        dr[2] = "Johnson";
        dr[3] = "mike@j.com";
        dt.Rows.Add(dr);
       
        dr = dt.NewRow();
        dr[0] = 2;
        dr[1] = "Rita";
        dr[2] = "Mjay";
        dr[3] = "rit@rit.com";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr[0] = 3;
        dr[1] = "Simran";
        dr[2] = "Off";
        dr[3] = "a@rev.com";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr[0] = 4;
        dr[1] = "Satalaj";
        dr[2] = "More";
        dr[3] = "satalaj@rev.com";
        dt.Rows.Add(dr);
       
        dr = dt.NewRow();
        dr[0] = 5;
        dr[1] = "Dinesh";
        dr[2] = "Zirpe";
        dr[3] = "satalaj@rev.com";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr[0] = 6;
        dr[1] = "Ajit";
        dr[2] = "Ekke";
        dr[3] = "Ajit@rev.com";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr[0] = 7;
        dr[1] = "Vivk";
        dr[2] = "Patil";
        dr[3] = "Patil@rev.com";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr[0] = 8;
        dr[1] = "Dhobale";
        dr[2] = "Dan";
        dr[3] = "satalaj@rev.com";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr[0] = 9;
        dr[1] = "Don";
        dr[2] = "Petus";
        dr[3] = "satalaj@rev.com";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr[0] = 10;
        dr[1] = "Shane";
        dr[2] = "Glov";
        dr[3] = "shane@rev.com";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr[0] = 11;
        dr[1] = "Shane";
        dr[2] = "Patson";
        dr[3] = "satalaj@rev.com";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr[0] = 12;
        dr[1] = "Dbriain";
        dr[2] = "Briane";
        dr[3] = "satalaj@rev.com";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr[0] = 13;
        dr[1] = "Mark";
        dr[2] = "Turner";
        dr[3] = "Mark@rev.com";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr[0] = 14;
        dr[1] = "Data";
        dr[2] = "More";
        dr[3] = "satalaj@rev.com";
        dt.Rows.Add(dr);

        return dt;


    }
}

 

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.