title: Edit Gridview Row ASP.net
body: <p>Struggling to edit a Gridview Row. Once I hit edit the gridview disappears, I guess because I only bind the grid on !IsPostBack - although the tutorial tells me to do it this way. </p>
  
  <p>I have tried rebinding on postback which lets me edit the gridview but keeps the default values - not the new values.</p>
  
  <p>Any help appreciated.</p>
  
  <pre><code>if (!IsPostBack)
  {
      if (Session["Order_ID"] != null)
      {
          showgrid(int.Parse(Session["Order_ID"].ToString()));
      }
  }
  
  public void showgrid(int Order_ID)
  {
      SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConn"].ToString());
  
      string sqlstring = "Removed - is working";
  
      SqlCommand cmd = new SqlCommand(sqlstring, conn);
      SqlDataAdapter da = new SqlDataAdapter(cmd);
      DataSet ds = new DataSet();
      da.Fill(ds);
      conn.Close();
  
      if (ds.Tables[0].Rows.Count &gt; 0)
      {
          gv_ExistingAds.DataSource = ds;
          gv_ExistingAds.DataBind();
      }
  }
  
  protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
  {
      gv_ExistingAds.EditIndex = e.NewEditIndex;
      showgrid(int.Parse(Session["Order_ID"].ToString()));
  }
  
  protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {
      Label lb = (Label)gv_ExistingAds.Rows[e.RowIndex].FindControl("Label3");
  
      TextBox tx1 = (TextBox)gv_ExistingAds.Rows[e.RowIndex].FindControl("TextBox3");
  
      string tx11 = tx1.Text;
  
      gv_ExistingAds.EditIndex = -1;
      showgrid(int.Parse(Session["Order_ID"].ToString()));
  }
  </code></pre>
  
  <p>HTML</p>
  
  <pre><code>&lt;asp:GridView runat="server" AutoGenerateColumns="false" ShowFooter="True" ShowHeaderWhenEmpty="false" EmptyDataText="This order has no ads..."
      ID="gv_ExistingAds" CssClass="table table-striped table-bordered table-hover"
      Visible="true" DataKeyNames="Orders_Editions_ID, Cancelled, Order_ID" EnableViewState="true" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"&gt;
      &lt;Columns&gt;
          &lt;asp:BoundField HeaderText="Publication Title" HtmlEncode="false" DataField="Publication_Title" /&gt;
          &lt;asp:BoundField HeaderText="Insertion Date" HtmlEncode="false" DataField="Insertion_Date" /&gt;
          &lt;asp:BoundField HeaderText="Advert Type" HtmlEncode="false" DataField="Description" /&gt;
          &lt;asp:BoundField DataField="Ad_Size" HeaderText="Size" ItemStyle-Width="150" /&gt;
          &lt;asp:BoundField DataField="Position" HeaderText="Position" ItemStyle-Width="150" /&gt;
          &lt;asp:BoundField DataField="Revenue" HeaderText="Revenue" ItemStyle-Width="150" /&gt;
          &lt;asp:TemplateField HeaderText="Note" ItemStyle-Width="150"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:Label ID="Label3" runat="server" Text='&lt;%# Eval("Note") %&gt;'&gt;&lt;/asp:Label&gt;
              &lt;/ItemTemplate&gt;
              &lt;EditItemTemplate&gt;
                  &lt;asp:TextBox ID="TextBox3" runat="server" Text='&lt;%# Eval("Note") %&gt;'&gt;&lt;/asp:TextBox&gt;
              &lt;/EditItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField HeaderText="Edit" ShowHeader="false"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:LinkButton ID="btnedit" runat="server" CommandName="Edit" Text="Edit"&gt;&lt;/asp:LinkButton&gt;
              &lt;/ItemTemplate&gt;
              &lt;EditItemTemplate&gt;
                  &lt;asp:LinkButton ID="btnupdate" runat="server" CommandName="Update" Text="Update"&gt;&lt;/asp:LinkButton&gt;
                  &lt;asp:LinkButton ID="btncancel" runat="server" CommandName="Cancel" Text="Cancel"&gt;&lt;/asp:LinkButton&gt;
              &lt;/EditItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField HeaderText="Action"&gt;
              &lt;ItemStyle Width="165px" /&gt;
              &lt;ItemTemplate&gt;
                  &lt;%--&lt;asp:LinkButton ID="btn_UpdateAdvert" ControlStyle-CssClass="btn btn-primary btn-xs" CommandName="Update_Advert" CommandArgument='&lt;%#Eval("Orders_Editions_ID")%&gt;' runat="server" Text="Update" CausesValidation="false" /&gt;--%&gt;
                  &lt;asp:LinkButton ID="btn_CancelAdvert" ControlStyle-CssClass="btn btn-warning btn-xs" CommandName="Cancel_Advert" CommandArgument='&lt;%#Eval("Orders_Editions_ID")%&gt;' runat="server" Text="Cancel" CausesValidation="false" /&gt;
                  &lt;asp:LinkButton ID="btn_DeleteAdvert" ControlStyle-CssClass="btn btn-danger btn-xs" CommandName="Delete_Advert" CommandArgument='&lt;%#Eval("Orders_Editions_ID")%&gt;' runat="server" Text="Delete" CausesValidation="false" /&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
      &lt;/Columns&gt;
  
      &lt;HeaderStyle BackColor="#FF6699" /&gt;
  
  &lt;/asp:GridView&gt;
  </code></pre>