title: How to Keep row values in gridview after adding new row?
body: <p>I have a gridview with textboxes for item no, desc qty, cost, extncost. When itemno entered in the textbox desc and cost of the item will comes automatically by on textbox event change.
  Here when i add a new row in the grid view, values of last entered value got disappears.
  when i am checking using break point i can able to see the last entered values in data table.
  Since, while adding new row has blank text boxes, now system considers blank text box for on text change event. So, last entered values also not displaying. Text box changes in one row also affects in other rows.</p>
  
  <p>Here is the ASPX.page code:</p>
  
  <pre><code>&lt;asp:UpdatePanel ID="gin_pnlupdt" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true"&gt;
                  &lt;ContentTemplate&gt;
                      &lt;asp:GridView ID="grv_gindet" runat="server" ShowFooter="True" AutoGenerateColumns="False"
                          CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="grvStudentDetails_RowDeleting"
                             OnRowDataBound="grv_gindtrowcmd" OnRowCommand="grv_gindetrowcmd"&gt;
                          &lt;Columns&gt;
                              &lt;asp:BoundField DataField="RowNumber" HeaderText="SNo" /&gt;
                              &lt;asp:TemplateField HeaderText="Item Number"&gt;
                                  &lt;ItemTemplate&gt;
                                      &lt;asp:TextBox ID="txt_itemno" runat="server" OnTextChanged="txt_itemno_changed" AutoPostBack="True"&gt;&lt;/asp:TextBox&gt;
                                  &lt;/ItemTemplate&gt;
                              &lt;/asp:TemplateField&gt;
                              &lt;asp:TemplateField HeaderText="Item Description"&gt;
                                  &lt;ItemTemplate&gt;
                                      &lt;asp:Label ID="txt_itemdesc" runat="server"&gt;&lt;/asp:Label&gt;
                                  &lt;/ItemTemplate&gt;
                              &lt;/asp:TemplateField&gt;
                              &lt;asp:TemplateField HeaderText="Quantity"&gt;
                                  &lt;ItemTemplate&gt;
                                      &lt;asp:TextBox ID="txt_qty" runat="server"&gt;&lt;/asp:TextBox&gt;
                                  &lt;/ItemTemplate&gt;
                              &lt;/asp:TemplateField&gt;
                              &lt;asp:TemplateField HeaderText="Unit Cost"&gt;
                                  &lt;ItemTemplate&gt;
                                      &lt;asp:Label ID="txt_ucost" runat="server"&gt;&lt;/asp:Label&gt;
                                  &lt;/ItemTemplate&gt;
                              &lt;/asp:TemplateField&gt;
                              &lt;asp:TemplateField HeaderText="Extended Cost"&gt;
                                  &lt;ItemTemplate&gt;
                                      &lt;asp:Label ID="txt_extncost" runat="server"&gt;&lt;/asp:Label&gt;
                                  &lt;/ItemTemplate&gt;
                                  &lt;FooterStyle HorizontalAlign="Right" /&gt;
                                  &lt;FooterTemplate&gt;
                                      &lt;asp:Button ID="ButtonAdd" runat="server" Text="Add New Row"  OnClick="BtnAddRow" /&gt;
                                  &lt;/FooterTemplate&gt;
                              &lt;/asp:TemplateField&gt;
                              &lt;asp:CommandField ShowDeleteButton="True" /&gt;
                          &lt;/Columns&gt;
                          &lt;FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /&gt;
                          &lt;RowStyle BackColor="#EFF3FB" /&gt;
                          &lt;EditRowStyle BackColor="#2461BF" /&gt;
                          &lt;SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /&gt;
                          &lt;PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /&gt;
                          &lt;HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /&gt;
                          &lt;AlternatingRowStyle BackColor="White" /&gt;
                      &lt;/asp:GridView&gt;
                  &lt;/ContentTemplate&gt;
                  &lt;Triggers&gt;
                      &lt;asp:AsyncPostBackTrigger ControlID="grv_gindet" /&gt;
                  &lt;/Triggers&gt;
              &lt;/asp:UpdatePanel&gt;
  </code></pre>
  
  <p>Here is the CS code for add new row:</p>
  
  <pre><code>protected void AddNewRow()
      {
  
          int rowIndex = 0;
  
          if (ViewState["CurrentTable"] != null)
          {
              DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
              DataRow drCurrentRow = null;
              if (dtCurrentTable.Rows.Count &gt; 0)
              {
                  for (int i = 1; i &lt;= dtCurrentTable.Rows.Count; i++)
                  {
                      TextBox TextBoxItem =
                        (TextBox)grv_gindet.Rows[rowIndex].Cells[1].FindControl("txt_itemno");
                      Label TextBoxDesc =
                        (Label)grv_gindet.Rows[rowIndex].Cells[2].FindControl("txt_itemdesc");
                      TextBox TextBoxQty =
                        (TextBox)grv_gindet.Rows[rowIndex].Cells[3].FindControl("txt_qty");
                      Label TextBoxucost =
                        (Label)grv_gindet.Rows[rowIndex].Cells[4].FindControl("txt_ucost");
                      Label TextBoxextncost =
                        (Label)grv_gindet.Rows[rowIndex].Cells[5].FindControl("txt_extncost");
                      drCurrentRow = dtCurrentTable.NewRow();
                      drCurrentRow["RowNumber"] = i + 1;
  
                      dtCurrentTable.Rows[i - 1]["Col1"] = TextBoxItem.Text;
                      dtCurrentTable.Rows[i - 1]["Col2"] = TextBoxDesc.Text;
                      dtCurrentTable.Rows[i - 1]["Col3"] = TextBoxQty.Text;
                      dtCurrentTable.Rows[i - 1]["Col4"] = TextBoxucost.Text;
                      dtCurrentTable.Rows[i - 1]["Col5"] = TextBoxextncost.Text;
                      rowIndex++;
                  }
                  dtCurrentTable.Rows.Add(drCurrentRow);
                  ViewState["CurrentTable"] = dtCurrentTable;
  
                  grv_gindet.DataSource = dtCurrentTable;
                  grv_gindet.DataBind();
              }
          }
          else
          {
              Response.Write("ViewState is null");
          }
          SetPreviousData();
  
      }
  </code></pre>
  
  <p>Code for retrive Previous data:</p>
  
  <pre><code>private void SetPreviousData()
      {
          int rowIndex = 0;
          if (ViewState["CurrentTable"] != null)
          {
              DataTable dt = (DataTable)ViewState["CurrentTable"];
              if (dt.Rows.Count &gt; 0)
              {
                  for (int i = 0; i &lt; dt.Rows.Count; i++)
                  {
                      TextBox TextBoxItem = (TextBox)grv_gindet.Rows[rowIndex].Cells[1].FindControl("txt_itemno");
                      Label TextBoxDesc = (Label)grv_gindet.Rows[rowIndex].Cells[2].FindControl("txt_itemdesc");
                      TextBox TextBoxQty =
                        (TextBox)grv_gindet.Rows[rowIndex].Cells[3].FindControl("txt_qty");
                      Label TextBoxucost =
                        (Label)grv_gindet.Rows[rowIndex].Cells[4].FindControl("txt_ucost");
                      Label TextBoxextncost =
                        (Label)grv_gindet.Rows[rowIndex].Cells[5].FindControl("txt_extncost");
  
                      TextBoxItem.Text = dt.Rows[i]["Col1"].ToString();
                      TextBoxDesc.Text = dt.Rows[i]["Col2"].ToString();
                      TextBoxQty.Text = dt.Rows[i]["Col3"].ToString();
                      TextBoxucost.Text = dt.Rows[i]["Col4"].ToString();
                      TextBoxextncost.Text = dt.Rows[i]["Col5"].ToString();
                      rowIndex++;
                  }
              }
          }
      }
  </code></pre>
  
  <p>Textbox change event:</p>
  
  <pre><code> protected void txt_itemno_changed(object sender, EventArgs e)
      {
  
          //TextBox thisTextBox = (TextBox)sender;
          //GridViewRow thisGridViewRow = (GridViewRow)thisTextBox.Parent.Parent;
          //int row = thisGridViewRow.RowIndex;
          GridViewRow currentrow = (GridViewRow)((TextBox)sender).Parent.Parent;
          TextBox thisTextBox = (TextBox)currentrow.FindControl("txt_itemno");
          int row = currentrow.RowIndex;
          //rowChanged[row] = true;
          using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AWCC"].ConnectionString))
          {
              con.Open();
              SqlCommand cmd = new SqlCommand("SELECT ITEMDET.ITEMDESC,RGITEMDET.UNITCOST FROM ITEMDET JOIN RGITEMDET ON RGITEMDET.ITEMNO=ITEMDET.ITEMNO WHERE ITEMDET.ITEMNO ='" + thisTextBox.Text + "' ", con);
              SqlDataReader dr = cmd.ExecuteReader();
              while (dr.Read())
              {
                  grv_gindet.Rows[row].Cells[2].Text = dr["ITEMDESC"].ToString();
                  grv_gindet.Rows[row].Cells[4].Text = dr["UNITCOST"].ToString();
              }
          }
          thisTextBox.Enabled = false;
  
      }
  </code></pre>
  
  <p>Postback control code for gridview child element:</p>
  
  <pre><code>        protected void grv_gindtrowcmd(object sender, GridViewRowEventArgs e)
      {
          try
          {
  
              TextBox txtitm = e.Row.FindControl("txt_itemno") as TextBox;
              LinkButton lnkbtn = e.Row.FindControl("ShowDeleteButton") as LinkButton;
              ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(txtitm);
              ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(lnkbtn);
  
          }
          catch
          {
  
          }
      }
      protected void grv_gindetrowcmd(object sender, GridViewCommandEventArgs e)
      {
          try
          {
              Button btnad = grv_gindet.FooterRow.FindControl("ButtonAdd") as Button;
              ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(btnad);
          }
          catch
          {
  
          }
      }
  </code></pre>
  
  <p>Kinldy provide a solution ASAP, do the needful.</p>