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. <asp:GridView ID="gvdashboard" ClientIDMode="Static" runat="server" class="table table-striped"
  22. AutoGenerateColumns="False" GridLines="None" CellPadding="4" ForeColor="#333333">
  23. <AlternatingRowStyle BackColor="White" />
  24. <Columns>
  25. <asp:TemplateField ItemStyle-Width="10px" HeaderStyle-Width="10px">
  26. <HeaderTemplate>
  27. <asp:CheckBox ID="chkSelectAll" CssClass="chkHeader" runat="server" />
  28. </HeaderTemplate>
  29. <ItemTemplate>
  30. <asp:CheckBox ID="chkRow" CssClass="chkItem" runat="server" />
  31. </ItemTemplate>
  32. <HeaderStyle Width="10px"></HeaderStyle>
  33. <ItemStyle Width="10px"></ItemStyle>
  34. </asp:TemplateField>
  35. <asp:BoundField DataField="ID" HeaderText="ID" ></asp:BoundField>
  36. <asp:BoundField DataField="Name" HeaderText="Name"></asp:BoundField>
  37. </Columns>
  38. <EditRowStyle BackColor="#2461BF" />
  39. <FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
  40. <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  41. <PagerStyle CssClass="pagination" BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
  42. <RowStyle BackColor="#EFF3FB" />
  43. <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
  44. <SortedAscendingCellStyle BackColor="#F5F7FB" />
  45. <SortedAscendingHeaderStyle BackColor="#6D95E1" />
  46. <SortedDescendingCellStyle BackColor="#E9EBEF" />
  47. <SortedDescendingHeaderStyle BackColor="#4870BE" />
  48. </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. }