title: Need to click the button twice to edit and update gridview data
body: <p>In my application i need to edit the gridview rows on clicking the edit button of that particular row everything works fine edit update and cancel the problem is when i click edit button my gridview disappears again to see the gridview i should click the below button</p>
  
  <p><code>&lt;asp:Button ID="Button1" runat="server" style="border:1px solid #456879;border-radius:5px;height: 22px;Width:150px" OnClick="Button1_Click" Text="Get Uploaded Data" Width="132px" /&gt;</code> 
  and similarly after updating my gridview disappears but update happens so to again see the gridview i should again click the above button Get Uploaded Data,Finally in order to edit gridview data i need to click the button twice and in order to see the updated data  again i need to click the button twice.How can i solve this</p>
  
  <pre><code>&lt;asp:GridView ID="GridView2" runat="server" CellPadding="3" Font-Size="12px" Width="300px" Visible="false" OnRowEditing="GridView2_RowEditing" OnRowUpdating="GridView2_RowUpdating" OnRowCommand="GridView2_RowCommand" OnRowCancelingEdit="GridView2_RowCancelingEdit" AutoGenerateColumns="False"  BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"&gt; 
                      &lt;Columns&gt; 
                           &lt;asp:TemplateField HeaderText="Action"&gt;
                          &lt;ItemTemplate&gt;
                             &lt;asp:ImageButton ID="imgbtnEdit" runat="server" CommandName="Edit" ImageUrl="Images/icon-edit.png" Height="32px" Width="32px"/&gt;
                          &lt;/ItemTemplate&gt;
                          &lt;EditItemTemplate&gt;
                             &lt;asp:ImageButton ID="imgbtnUpdate" runat="server" CommandName="Update" ImageUrl="Images/update1.jpg"/&gt;
                             &lt;asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="Images/cancel.jpg"/&gt;
                          &lt;/EditItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;             
                          &lt;asp:templatefield headertext="sno"&gt;
                          &lt;itemtemplate&gt;
                              &lt;asp:label id="lblid" runat="server" text='&lt;%#DataBinder.Eval(Container.DataItem, "ID") %&gt;'&gt;&lt;/asp:label&gt;
                          &lt;/itemtemplate&gt;
                          &lt;edititemtemplate&gt;           
                              &lt;asp:label id="lbleditid" runat="server" text='&lt;%#DataBinder.Eval(Container.DataItem, "ID") %&gt;'&gt;&lt;/asp:label&gt;           
                          &lt;/edititemtemplate&gt;
                      &lt;/asp:templatefield&gt;    
                        &lt;asp:TemplateField HeaderText="Name"&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Label ID="lblName" runat="server" Text='&lt;%#DataBinder.Eval(Container.DataItem, "Name") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;
                          &lt;EditItemTemplate&gt;           
                              &lt;asp:TextBox ID="txtEditName" runat="server" Text='&lt;%#DataBinder.Eval(Container.DataItem, "Name") %&gt;'&gt;&lt;/asp:TextBox&gt;           
                          &lt;/EditItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                            &lt;asp:TemplateField HeaderText="Salary"&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Label ID="lblSalary" runat="server" Text='&lt;%#DataBinder.Eval(Container.DataItem, "Salary") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;
                          &lt;EditItemTemplate&gt;           
                              &lt;asp:TextBox ID="txtEditSalary" runat="server" Text='&lt;%#DataBinder.Eval(Container.DataItem, "Salary") %&gt;'&gt;&lt;/asp:TextBox&gt;           
                          &lt;/EditItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                            &lt;asp:TemplateField HeaderText="Designation"&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Label ID="lblDesignation" runat="server" Text='&lt;%#DataBinder.Eval(Container.DataItem, "Designation") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;
                          &lt;EditItemTemplate&gt;           
                              &lt;asp:TextBox ID="txtEditDesignation" runat="server" Text='&lt;%#DataBinder.Eval(Container.DataItem, "Designation") %&gt;'&gt;&lt;/asp:TextBox&gt;           
                          &lt;/EditItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                            &lt;asp:TemplateField HeaderText="Location"&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Label ID="lblLocation" runat="server" Text='&lt;%#DataBinder.Eval(Container.DataItem, "Location") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;
                          &lt;EditItemTemplate&gt;           
                              &lt;asp:TextBox ID="txtEditLocation" runat="server" Text='&lt;%#DataBinder.Eval(Container.DataItem, "Location") %&gt;'&gt;&lt;/asp:TextBox&gt;           
                          &lt;/EditItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                  &lt;/Columns&gt;           
                   &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>.Cs Code</p>
  
  <pre><code>protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
      {
          GridView2.EditIndex = e.NewEditIndex;
          GridView2.DataBind();
          GridView2.Visible = true;
  
      }
      protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
      {
          Label lblEditID = (Label)GridView2.Rows[e.RowIndex].FindControl("lblEditID");
          TextBox txtEditName = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txtEditName");
          TextBox txtEditSalary = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txtEditSalary");
          TextBox txtEditDesignation = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txtEditDesignation");
          TextBox txtEditLocation = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txtEditLocation");
          con.Open();
          string cmdstr = "update CodingLog1 set Name=@Name,Salary=@Salary,Designation=@Designation,Location=@Location where ID=@ID";
          SqlCommand cmd = new SqlCommand(cmdstr, con);
          cmd.Parameters.AddWithValue("@ID", lblEditID.Text);
          cmd.Parameters.AddWithValue("@Name", txtEditName.Text);
          cmd.Parameters.AddWithValue("@Salary", txtEditSalary.Text);
          cmd.Parameters.AddWithValue("@Designation", txtEditDesignation.Text);
          cmd.Parameters.AddWithValue("@Location", txtEditLocation.Text);
          cmd.ExecuteNonQuery();
          con.Close();
          GridView2.EditIndex = -1;
          GridView2.Visible = true;
          GridView2.DataBind();
      }
      protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
      {
  
      }
      protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
      {
            GridView2.EditIndex = -1;
            GridView2.Visible = true;
            GridView2.DataBind();
      }
  </code></pre>
  
  <p>Button code-</p>
  
  <pre><code> protected void Button1_Click(object sender, EventArgs e)
      {
          GridView1.DataSource = null; GridView1.DataBind();
          string desi = Session["Role"].ToString();
          string user = Session["Username"].ToString();
          string selecteduser = ddlusers.SelectedItem.Text;
  
          if (TextBox1.Text != "")
          {
              if (rdupldeddate.Checked == true)
              {
                  DataTable dt = adm.GetRecordsByUploadedDate(user, TextBox1.Text, desi, selecteduser);  //Uploaded date
  
                  if (dt.Rows.Count &gt; 0)
                  {
                      if (desi == "Supervisor")
                      {
                          BtnExport.Visible = true;
                          GridView2.Visible = true;
                          GridView1.Visible = false;
                          GridView2.DataSource = dt;
                          GridView2.DataBind();
                      }
                      else
                      {
                          GridView2.Visible = false;
                          BtnExport.Visible = false;
                          GridView1.Visible = true;
                          GridView1.DataSource = dt;
                          GridView1.DataBind();
                      }
                  }
                  else { lblMsg.Visible = true; GridView1.Visible = false; GridView2.Visible = false; lblMsg.Text = "No Data Present!!!"; }
              }
              else
              {
                  DataTable dt = adm.GetRecordsByCodedDate(user, TextBox1.Text, desi, selecteduser);  //CodedDate
                  if (dt.Rows.Count &gt; 0)
                  {
                      if (desi == "Supervisor")
                      {
                          BtnExport.Visible = true;
                          GridView2.Visible = true;
                          GridView1.Visible = false;
                          GridView2.DataSource = dt;
                          GridView2.DataBind();
                      }
                      else
                      {
                          GridView2.Visible = false;
                          BtnExport.Visible = false;
                          GridView1.Visible = true;
                          GridView1.DataSource = dt;
                          GridView1.DataBind();
                      }
                  }
                  else { lblMsg.Visible = true; GridView1.Visible = false; GridView2.Visible = false; lblMsg.Text = "No Data Present!!!"; }
              }
          }
          else { lblMsg.Visible = true; lblMsg.Text = "Please Enter Date!!!"; }
  
      }
  </code></pre>
  
  <p>I had tried all the possibilties by taking editindex=0 instead of -1, by calling my gridview2.databind in !ispostback moreover i had placed my gridview in update panel also but still its not working</p>