Z5 Concepts

ASP.NET C#.NET GridViewRow with ASP.NET CheckBox control


by on 19. May 2011 13:55

When developing with C#.NET in Visual Studio, developers work with the ASP.NET GridView control, there are many cases when needing the ability to check each row in a GridView in order to call a function on button click or even on CheckChanged. In my case, I was needing to allow for the user to delete multiple records at once, either by selecting all or by individually checking each check box.

 

So, here we go:


In the .aspx page where you have your GridView control, you should have the following:

 

 

<asp:GridView ID="GridView1" runat="server" DataSourceID="DSN">

 

<Columns>

 

</Columns>

 

</asp:GridView>

 

 

 

 

Inside the <Columns> node, you should add the following code:


 

     <asp:TemplateField>

      <HeaderTemplate>

       <asp:CheckBox ID="cbSelectAll" runat="server" Text="All" AutoPostBack="true"

                     OnCheckedChanged="chkSelectAll_CheckedChanged" />

      </HeaderTemplate>

      <ItemTemplate>

         <asp:CheckBox ID="cb" runat="server"></asp:CheckBox>

      </ItemTemplate>

     </asp:TemplateField>

 

 

 

 

 

After adding the above code, it should look as so:


 

 

 

    <asp:GridView ID="GridView1" runat="server" DataSourceID="DSN">

 

    <Columns>

     <asp:TemplateField>

      <HeaderTemplate>

         <asp:CheckBox ID="cbSelectAll" runat="server"

                       Text="All" AutoPostBack="true"

                       OnCheckedChanged="chkSelectAll_CheckedChanged" />

      </HeaderTemplate>

      <ItemTemplate>

         <asp:CheckBox ID="cb" runat="server"></asp:CheckBox>

      </ItemTemplate>

     </asp:TemplateField>

    </Columns>

 

    </asp:GridView>

 

 

 

 

 

Now, in the code behind file, the .aspx.cs file, you will need to add the following method(s) in order to allow for select all on that column and a button click event. See below:


    

    protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)

    {

        CheckBox checkbox;

        foreach (GridViewRow row in GridView1.Rows)

        {

            checkbox = (CheckBox)(row.Cells[0].FindControl("cb"));

            checkbox.Checked = ((CheckBox)sender).Checked;

        }

    }


In my particular example, I had a button on the .aspx page that was clicked once all rows were checked, to fire and grab the DataKey of each row and update the record in the database, so you may or may not find the following useful.

   

    protected void btnDeleteSelected_Click(object sender, EventArgs e)

    {

        foreach (GridViewRow r in GridView1.Rows)

        {

            String ID = GridView1.DataKeys[r.RowIndex].Values["ID"].ToString();

            CheckBox cb = ((CheckBox)r.Cells[0].FindControl("cb"));

            if (cb.Checked)

            {

                //write your SQL Connection code here

            }

        }

        GridView1.DataBind();

    }

 

 

 

 

If you have any questions or problems, feel free to e-mail me at DJ@z5concepts.com

Tags:

ASP.NET | C#.NET | Troubleshooting | Visual Studio 2005 | Visual Studio 2008 | Web hosting | Websites

Comments are closed


Theme by Z5 Concepts