Please refer the below code example. This will give you better idea.
01.<%@ Page Language="C#" %>
02.
03.<%@ Import Namespace="System" %>
04.<%@ Import Namespace="System.Data" %>
05.<%@ Import Namespace="System.Data.SqlClient" %>
06.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
07.<script runat="server">
08. protected void Page_Load(object sender, EventArgs e)
09. {
10. if (!IsPostBack)
11. LoadProducts();
12. }
13.
14.
15. private void LoadProducts()
16. {
17. using (SqlConnection connection = new SqlConnection("ConnectionString"))
18. {
19. using (SqlCommand command = new SqlCommand("Select * from Products", connection))
20. {
21. using (SqlDataAdapter da = new SqlDataAdapter(command)) {
22. DataTable dt = new DataTable();
23. da.Fill(dt);
24. GridView1.DataSource = dt;
25. GridView1.DataBind();
26. }
27. }
28. }
29. }
30.
31. protected void cb_CheckedChanged(object sender, EventArgs e)
32. {
33. using (SqlConnection connection = new SqlConnection("ConnectionString"))
34. {
35. using (SqlCommand command = new SqlCommand("Update products set Status=@Status where ProductName=@ProductName", connection))
36. {
37. command.Parameters.AddWithValue("@Status", ((CheckBox)sender).Checked.ToString());
38. command.Parameters.AddWithValue("@ProductName", ((CheckBox)sender).Text);
39. connection.Open();
40. command.ExecuteNonQuery();
41. connection.Close();
42. }
43. }
44. }
45.
46. protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
47. {
48. if (e.Row.RowType == DataControlRowType.DataRow)
49. {
50. HiddenField hd = e.Row.FindControl("hdn") as HiddenField;
51. CheckBox cb = e.Row.FindControl("cb") as CheckBox;
52. if (hd.Value.ToLower() == "true")
53. cb.Checked = true;
54. else
55. cb.Checked = false;
56. }
57. }
58.</script>
59.<html xmlns="http://www.w3.org/1999/xhtml">
60.<head runat="server">
61. <title></title>
62.</head>
63.<body>
64. <form id="form1" runat="server">
65. <div>
66. <asp:ScriptManager ID="sm" runat="server">
67. </asp:ScriptManager>
68. <asp:GridView ID="GridView1" runat="server"
69. onrowdatabound="GridView1_RowDataBound">
70. <Columns>
71. <asp:TemplateField>
72. <ItemTemplate>
73. <asp:CheckBox id="cb" runat="server" AutoPostBack="true" Text='<%#Eval("ProductName") %>' OnCheckedChanged="cb_CheckedChanged" />
74. <asp:HiddenField ID="hdn" runat="server" Value='<%#Eval("Status") %>' />
75. </ItemTemplate>
76. </asp:TemplateField>
77. </Columns>
78. </asp:GridView>
79. <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
80. SelectCommand="SELECT [ProductID], [ProductName], [QuantityPerUnit], [UnitPrice], [UnitsInStock] FROM [Alphabetical list of products]">
81. </asp:SqlDataSource>
82. </div>
83. </form>
84.</body>
85.</html>