Add a Checkbox in ASP GridView with the Select All Functionality

Introduction

This code will show you how to add a checkbox in ASP Gridview with the select all functionality. It also shows how to get selected checkbox Gridview row values. I have added only two columns in Gridview, but you can use the code as per your requirement.
 
Code
 
.aspx page code
 
Add a Javascript reference to your page:
  1. <script type="text/javascript" src="jquery.js"></script>  
  2. <script type="text/javascript">  
  3.    $(document).ready(function () {  
  4.       var headerChk = $(".chkHeader input");  
  5.       var itemChk = $(".chkItem input");  
  6.          headerChk.click(function () {  
  7.             itemChk.each(function () {  
  8.                this.checked = headerChk[0].checked;  
  9.          })  
  10.    });  
  11.    itemChk.each(function () {  
  12.          $(this).click(function () {  
  13.             if (this.checked == false)  
  14.             {  
  15.                headerChk[0].checked = false;  
  16.              }  
  17.             })  
  18.          });  
  19.    });  
  20. </script>  
  21.    
  22. <asp:GridView ID="gvdashboard" ClientIDMode="Static" runat="server" class="table table-striped"  
  23. AutoGenerateColumns="False" GridLines="None" CellPadding="4" ForeColor="#333333">  
  24. <AlternatingRowStyle BackColor="White" />  
  25.    <Columns>  
  26.       <asp:TemplateField ItemStyle-Width="10px" HeaderStyle-Width="10px">  
  27.          <HeaderTemplate>  
  28.             <asp:CheckBox ID="chkSelectAll" CssClass="chkHeader" runat="server" />  
  29.          </HeaderTemplate>  
  30.       <ItemTemplate>  
  31.       <asp:CheckBox ID="chkRow" CssClass="chkItem" runat="server" />  
  32.       </ItemTemplate>  
  33.       <HeaderStyle Width="10px"></HeaderStyle>  
  34.          <ItemStyle Width="10px"></ItemStyle>  
  35.      </asp:TemplateField>  
  36.      <asp:BoundField DataField="ID" HeaderText="ID" ></asp:BoundField>  
  37.      <asp:BoundField DataField="Name" HeaderText="Name"></asp:BoundField>  
  38. </Columns>  
  39.    <EditRowStyle BackColor="#2461BF" />  
  40.    <FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />  
  41.    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />  
  42.    <PagerStyle CssClass="pagination" BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />  
  43.    <RowStyle BackColor="#EFF3FB" />  
  44.    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />  
  45.    <SortedAscendingCellStyle BackColor="#F5F7FB" />  
  46.    <SortedAscendingHeaderStyle BackColor="#6D95E1" />  
  47.    <SortedDescendingCellStyle BackColor="#E9EBEF" />  
  48.    <SortedDescendingHeaderStyle BackColor="#4870BE" />  
  49. </asp:GridView>     

.aspx.cs page code 
  1. foreach (GridViewRow row in gvdashboard.Rows)  
  2. {  
  3.    if (row.RowType == DataControlRowType.DataRow)  
  4.       {  
  5.          CheckBox chkRow = (row.Cells[0].FindControl("chkRow"as CheckBox);  
  6.       if (chkRow.Checked)  
  7.       {  
  8.          // do whatever your logic  
  9.       }  
  10.    }  
  11. }