blog

Home / DeveloperSection / Blogs / How to Select all checkbox in griedview in asp.net?

How to Select all checkbox in griedview in asp.net?

gautam sen5040 18-Apr-2014
//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;
        }
       
    }
             

Updated 18-Sep-2014
hi, I'm a beginner, want to learn more and I'm on a dot-net platform.

Leave Comment

Comments

Liked By