Home > DeveloperSection > Blogs > How to select all checkbox in gridview in asp.net using c#

How to select all checkbox in gridview in asp.net using c#


ASP.Net ASP.Net 
Ratings:
1 Comment(s)
 6358  View(s)
Rate this:
//default.aspx
//First step:Take a grid view and checkbox
           <asp:GridView ID="GridView1" runat="server" Height="131px" Width="500px" DataKeyNames="Id"
            AutoGenerateColumns="False" Style="margin-top: 2px; margin-left: 0px; margin-bottom: 1px;">
            <Columns>
                <asp:TemplateField HeaderText="Action">
                    <ItemTemplate>
                        <asp:CheckBox ID="chkRow" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </columns>


//default.cs
//second step:take a button for select all checkbox
             //double click on button and write code
protected void Button2_Click(object sender, EventArgs e)
    {
        CheckState(b);//this is function 
    }
//make global variable
  bool b=true;
//make a function
 private void CheckState(bool p)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox chkcheck = (CheckBox)row.FindControl("chkRow");
            chkcheck.Checked = p;
        }
       
    }
             

Nice Blog

By Kamlakar Singh on   2 years ago
Hi gautam sen  this blog is very helpful for me

Don't want to miss updates? Please click the below button!

Follow MindStick