title: How to bind a GridView from code behind and update the same
body: <p>I check many sites and referred many codes before I could post a questions here. I am facing lot of confusions seeing them. Here is my problem.</p>
  
  <p>I have a <code>GridView</code> and I have bound it from code behind as :</p>
  
  <pre><code>public void BindData()
  {
      SqlCommand comd = new SqlCommand("SELECT * FROM " + Label2.Text + "", con);
      SqlDataAdapter da = new SqlDataAdapter(comd);
      DataTable dt = new DataTable();
      da.Fill(dt);
      GridView2.DataSource = dt;
      GridView2.DataBind();
  }
  </code></pre>
  
  <p>And my asp.net for the same looks like :</p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server" ForeColor="#333333" 
                  AutoGenerateEditButton="True" DataKeyNames="Locations" 
                  onrowcancelingedit="GridView1_RowCancelingEdit" 
                  onrowdatabound="GridView1_RowDataBound" 
                  onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"&gt;
      &lt;FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /&gt;
      &lt;RowStyle BackColor="#F7F6F3" ForeColor="#333333" /&gt;
  
      &lt;Columns&gt;
          &lt;asp:TemplateField HeaderText="Locations"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:Label ID="LblLocations" runat="server" Text='&lt;%#Eval("Locations") %&gt;'&gt;&lt;/asp:Label&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
  
          &lt;asp:TemplateField HeaderText="Lamp_Profile1"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:Label ID="LblLamp_Profile1" runat="server" Text='&lt;%#Eval("Lamp_Profile1") %&gt;'&gt;&lt;/asp:Label&gt;
              &lt;/ItemTemplate&gt;
              &lt;EditItemTemplate&gt;
                  &lt;asp:Label ID="LblEditLamp_Profile1" runat="server" Text='&lt;%#Eval("Lamp_Profile1") %&gt;'&gt;&lt;/asp:Label&gt;
              &lt;/EditItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
  
          &lt;asp:TemplateField HeaderText="Fan_Profile1"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:Label ID="LblFan_Profile1" runat="server" Text='&lt;%#Eval("Fan_Profile1") %&gt;'&gt;&lt;/asp:Label&gt;
              &lt;/ItemTemplate&gt;
              &lt;EditItemTemplate&gt;
                  &lt;asp:Label ID="LblEditFan_Profile1" runat="server" Text='&lt;%#Eval("Fan_Profile1") %&gt;'&gt;&lt;/asp:Label&gt;
              &lt;/EditItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
  
          &lt;asp:TemplateField HeaderText="AC_Profile1"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:Label ID="LblAC_Profile1" runat="server" Text='&lt;%#Eval("AC_Profile1") %&gt;'&gt;&lt;/asp:Label&gt;
              &lt;/ItemTemplate&gt;
              &lt;EditItemTemplate&gt;
                  &lt;asp:Label ID="LblEditAC_Profile1" runat="server" Text='&lt;%#Eval("AC_Profile1") %&gt;'&gt;&lt;/asp:Label&gt;
              &lt;/EditItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
      &lt;/Columns&gt;
  
      &lt;PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /&gt;
      &lt;SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /&gt;
      &lt;HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /&gt;
      &lt;EditRowStyle BackColor="#999999" /&gt;
      &lt;AlternatingRowStyle BackColor="White" ForeColor="#284775" /&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>And I have written my <code>GridView1_RowCancelingEdit</code> like:</p>
  
  <pre><code>protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  {
      e.Cancel = true;
      GridView1.EditIndex = -1;
  }
  </code></pre>
  
  <p>And my <code>GridView1_RowEditing</code> looks like:</p>
  
  <pre><code>protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
  {
      GridView1.EditIndex = e.NewEditIndex;
      BindData();
  }
  </code></pre>
  
  <p>And my <code>GridView1_RowUpdating</code> looks like:</p>
  
  <pre><code>protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {
      GridView1.EditIndex = e.RowIndex;
  
      Label Locations = GridView1.Rows[e.RowIndex].FindControl("LblLocations") as Label;
      //ViewState["Locations_Instance"] = Locations.Text;
  
      Label Lamp_Profile1 = GridView1.Rows[e.RowIndex].FindControl("LblLamp_Profile1") as Label;
      Label Fan_Profile1 = GridView1.Rows[e.RowIndex].FindControl("LblFan_Profile1") as Label;
      Label AC_Profile1 = GridView1.Rows[e.RowIndex].FindControl("LblAC_Profile1") as Label;
  
      string query = "UPDATE " + Label3.Text + " SET Lamp_Profile1 ='" + Lamp_Profile1 + "', Fan_Profile1 ='" + Fan_Profile1 + "', AC_Profile1 ='" + AC_Profile1 + "' WHERE Locations = '" + Locations + "'";
      com = new SqlCommand(query, con);
      con.Open();
      com.ExecuteNonQuery();
      con.Close();
      GridView1.EditIndex = -1;
  
      BindData();
      //lbldisplay.Text = "Updated Successfully";
  }
  </code></pre>
  
  <p><strong>From this I am getting what ever row I am binding using template field as well as database columns in my <code>GridView</code> and Once I click on Edit in the <code>GridView</code>, the whole <code>GridView</code> disappears.</strong></p>
  
  <p>Please help me.</p>