title: Checkbox in Gridview always returns false, ASP.NET, WEBFORM
body: <p>I am developing a WebForm with Gridview to show a data that client will retrieve. But I have a problem with updating data in Gridview, after client checks the CheckBox hi will press a button to update the value in the database. But my checkbox always returns FALSE. 
  I use .Net framework 4</p>
  
  <pre><code>public void Button_Submit_Onclick(object sender, EventArgs e)
      {
  
          for (int i = 0; i &lt; GridView2.Rows.Count; i++)
          {
              con.ConnectionString = ConfigurationManager.ConnectionStrings["TestDeductionsConnectionString2"].ToString();
              int recordid = Convert.ToInt32(GridView2.DataKeys[i].Values[0]);
              bool private1 = Convert.ToBoolean(GridView2.FindControl("CheckBox1"));
  
              SqlCommand cmd = new SqlCommand();
              cmd.Connection = con;
              cmd.CommandText = "Update DetailCosts set  private='" + private1 + "' where recordid=" + recordid;
              con.Open();
              if (private1 == true)
              {
  
                  cmd.Parameters.AddWithValue("@private1", SqlDbType.Bit).Value = private1;
                  cmd.Parameters.AddWithValue("@recordid", SqlDbType.Int).Value = recordid.ToString();
              }
              else
              {
                  cmd.Parameters.AddWithValue("@recordid", SqlDbType.Int).Value = recordid.ToString();
                  cmd.Parameters.Add("@private1", SqlDbType.Bit).Value = private1;
              }
              cmd.ExecuteNonQuery();
              con.Close();
          }}
  </code></pre>
  
  <p>Here is a code for Gridview.</p>
  
  <pre><code>&lt;asp:GridView ID="GridView2" runat="server"  AutoGenerateColumns="False" DataSourceID="SqlDataSource2" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical" DataKeyNames="recordid"&gt;
                              &lt;AlternatingRowStyle BackColor="#CCCCCC" /&gt;
                              &lt;Columns&gt;
                                   &lt;asp:BoundField DataField="recordid" HeaderText="recordid" SortExpression="recordid"&gt;
                                  &lt;/asp:BoundField&gt;
                                  &lt;asp:BoundField DataField="Employee" HeaderText="Employee" SortExpression="Employee"&gt;
                                  &lt;/asp:BoundField&gt;
                                  &lt;asp:BoundField DataField="mesec" HeaderText="Month" SortExpression="mesec"&gt;
                                      &lt;ItemStyle HorizontalAlign="Center" /&gt;
                                  &lt;/asp:BoundField&gt;
                                  &lt;asp:BoundField DataField="godina" HeaderText="Year" SortExpression="godina"&gt;
                                      &lt;ItemStyle HorizontalAlign="Center" /&gt;
                                  &lt;/asp:BoundField&gt;
                                  &lt;asp:BoundField DataField="CallDate" HeaderText="Call Date" SortExpression="CallDate"&gt;
                                      &lt;ItemStyle HorizontalAlign="Center" /&gt;
                                  &lt;/asp:BoundField&gt;
                                  &lt;asp:BoundField DataField="PhoneType" HeaderText="Phone Type" SortExpression="PhoneType"&gt;
                                      &lt;ItemStyle HorizontalAlign="Center" /&gt;
                                  &lt;/asp:BoundField&gt;
                                  &lt;asp:BoundField DataField="land" HeaderText="Land" SortExpression="land"&gt;
                                      &lt;ItemStyle HorizontalAlign="Center" /&gt;
                                  &lt;/asp:BoundField&gt;
                                  &lt;asp:BoundField DataField="number" HeaderText="Number" SortExpression="number"&gt;
                                      &lt;ItemStyle HorizontalAlign="Center" /&gt;
                                  &lt;/asp:BoundField&gt;
                                  &lt;asp:BoundField DataField="Minutes" HeaderText="Minutes" SortExpression="Minutes"&gt;
                                      &lt;ItemStyle HorizontalAlign="Center" /&gt;
                                  &lt;/asp:BoundField&gt;
                                  &lt;asp:BoundField DataField="Costs" HeaderText="Costs" SortExpression="Costs"&gt;
                                      &lt;ItemStyle HorizontalAlign="Center" /&gt;
                                  &lt;/asp:BoundField&gt;
                                  &lt;asp:BoundField DataField="privateCost" HeaderText="Private Costs" SortExpression="privateCost"&gt;
                                      &lt;ItemStyle HorizontalAlign="Center" /&gt;
                                  &lt;/asp:BoundField&gt;
                                  &lt;asp:TemplateField HeaderText="Private" SortExpression="private" &gt;
                                      &lt;ItemTemplate&gt;
                                          &lt;asp:CheckBox  ID="CheckBox1" runat="server" Checked='&lt;%# Eval("private") %&gt;' /&gt;
                                      &lt;/ItemTemplate&gt;
                                      &lt;HeaderStyle HorizontalAlign="Center" /&gt;
                                  &lt;/asp:TemplateField&gt;
                                  &lt;asp:BoundField DataField="Komentar" HeaderText="Commnet" SortExpression="Komentar"&gt;
                                      &lt;ItemStyle HorizontalAlign="Center" /&gt;
                                  &lt;/asp:BoundField&gt;
                              &lt;/Columns&gt;
                              &lt;FooterStyle BackColor="#CCCCCC" /&gt;
                              &lt;HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /&gt;
                              &lt;PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" /&gt;
                              &lt;SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /&gt;
                              &lt;SortedAscendingCellStyle BackColor="#F1F1F1" /&gt;
                              &lt;SortedAscendingHeaderStyle BackColor="#808080" /&gt;
                              &lt;SortedDescendingCellStyle BackColor="#CAC9C9" /&gt;
                              &lt;SortedDescendingHeaderStyle BackColor="#383838" /&gt;
                          &lt;/asp:GridView&gt;
  </code></pre>