Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Select all rows from one checkbox in a GridView

0.00/5 (No votes)
12 May 2011 1  
Today i'm going to show you how to select all records in a gridview from one checkbox which located in gridview header.First set gridview like below

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.


Today i'm going to show you how to select all records in a gridview from one checkbox which located in gridview header.

First set gridview like below code snnippet

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            onrowdatabound="GridView1_RowDataBound"
            
            >
            <Columns>
                <asp:TemplateField>
                
                <HeaderTemplate>
                <asp:CheckBox ID="allchk" runat="server" Text="All" />
                </HeaderTemplate>
                <ItemTemplate>
                
                <asp:CheckBox ID="selectchk" runat="server" />
               
                </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="TR. ID">
                
                <ItemTemplate>
                <asp:Label ID="namelbl" runat="server" Text='<%#Eval("name") %>'></asp:Label>
                </ItemTemplate>
                 </asp:TemplateField>
                
            </Columns>
        </asp:GridView>

In the next step you have to write small java script code.


<script type="text/javascript">
        function SelectAll(id)
        {
            //get reference of GridView control
            var grid = document.getElementById("<%= GridView1.ClientID %>");
            //variable to contain the cell of the grid
            var cell;
            
            if (grid.rows.length > 0)
            {
                //loop starts from 1. rows[0] points to the header.
                for (i=1; i<grid.rows.length; i++)
                {
                    //get the reference of first column
                    cell = grid.rows[i].cells[0];
                    
                    //loop according to the number of childNodes in the cell
                    for (j=0; j<cell.childNodes.length; j++)
                    {           
                        //if childNode type is CheckBox                 
                        if (cell.childNodes[j].type =="checkbox")
                        {
                        //assign the status of the Select All checkbox to the cell checkbox within the grid
                            cell.childNodes[j].checked = document.getElementById(id).checked;
                        }
                    }
                }
            }
        }
    </script>

Next, in gridview rowdatabound event add javascript function to the all select checkbox onclick event.

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
           //header select all function
            if (e.Row.RowType == DataControlRowType.Header)
            {
                ((CheckBox)e.Row.FindControl("allchk")).Attributes.Add("onclick", "javascript:SelectAll('" +
                    ((CheckBox)e.Row.FindControl("allchk")).ClientID + "')");


            }

            }

Happy programming!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here