title: Asp.Net change value of textbox in grid on selected Index changed
body: <p><img src="https://i.stack.imgur.com/NcJGd.png" alt="enter image description here"></p>
  
  <p>I have this grid on which I am dynamically adding controls. Now I want to change the respective value of Cost Price and Sale Price on Products selected index changed event. The problem is I cannot get the specific row. Below the code I have done so far.</p>
  
  <pre><code>    &lt;asp:GridView ID="grdSales" runat="server"
                      ShowFooter="True" AutoGenerateColumns="False"
                      CellPadding="4" CssClass="table table-hover table-striped"
                      GridLines="None" OnRowDeleting="grdSales_RowDeleting" OnRowCommand="grdSales_RowCommand"&gt;
                      &lt;Columns&gt;
                          &lt;asp:BoundField DataField="RowNumber" HeaderText="SNo" /&gt;
  
                          &lt;asp:TemplateField HeaderText="Products"&gt;
                              &lt;ItemTemplate&gt;
                                  &lt;asp:DropDownList ID="ddlProducts" runat="server"  CssClass="grid-control form-control" OnSelectedIndexChanged="ddlProducts_SelectedIndexChanged"&gt;
                                  &lt;/asp:DropDownList&gt;
                              &lt;/ItemTemplate&gt;
                          &lt;/asp:TemplateField&gt;
  
  
                          &lt;asp:TemplateField HeaderText="Cost Price"&gt;
                              &lt;ItemTemplate&gt;
                                  &lt;asp:TextBox ID="txtCp" runat="server" CssClass="grid-control form-control"&gt;&lt;/asp:TextBox&gt;
                              &lt;/ItemTemplate&gt;
                          &lt;/asp:TemplateField&gt;
                            &lt;asp:TemplateField HeaderText="Sale Price"&gt;
                              &lt;ItemTemplate&gt;
                                  &lt;asp:TextBox ID="txtSp" runat="server" CssClass="grid-control form-control"&gt;&lt;/asp:TextBox&gt;
                              &lt;/ItemTemplate&gt;
                          &lt;/asp:TemplateField&gt;
                          &lt;asp:TemplateField HeaderText="Total Units"&gt;
                              &lt;ItemTemplate&gt;
                                  &lt;asp:TextBox ID="txtUnits" runat="server" CssClass="grid-control form-control"&gt;&lt;/asp:TextBox&gt;
                              &lt;/ItemTemplate&gt;
                                &lt;FooterStyle HorizontalAlign="Left" /&gt;
                              &lt;FooterTemplate&gt;
                                 Total Journal Cost: &lt;asp:Label ID="lblTotalCost" runat="server" Text="0"&gt;&lt;/asp:Label&gt;
                              &lt;/FooterTemplate&gt;
                          &lt;/asp:TemplateField&gt;
                          &lt;asp:TemplateField HeaderText="Total Cost"&gt;
                              &lt;ItemTemplate&gt;
                                  &lt;asp:TextBox ID="txtTotal" runat="server" CssClass="grid-control form-control"&gt;&lt;/asp:TextBox&gt;
                              &lt;/ItemTemplate&gt;
  
  
  
  
                               &lt;FooterStyle HorizontalAlign="Left" /&gt;
                              &lt;FooterTemplate&gt;
                                  &lt;asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" CssClass="btn btn-primary" OnClick="ButtonAdd_Click" /&gt;
                              &lt;/FooterTemplate&gt;
                          &lt;/asp:TemplateField&gt;
  
                          &lt;asp:CommandField
                              ShowDeleteButton="True" /&gt;
                      &lt;/Columns&gt;
  
  
                  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>and my code behind is</p>
  
  <pre><code> #region Private Functions
      private void FirstGridViewRow()
      {
          DataTable dt = new DataTable();
          DataRow dr = null;
          dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
  
          dt.Columns.Add(new DataColumn("Products", typeof(string)));
  
          dt.Columns.Add(new DataColumn("CostPrice", typeof(string)));
          dt.Columns.Add(new DataColumn("SalePrice", typeof(string)));
          dt.Columns.Add(new DataColumn("TotalUnits", typeof(string)));
          dt.Columns.Add(new DataColumn("TotalCost", typeof(string)));
          dr = dt.NewRow();
  
          dr["RowNumber"] = 1;
  
          dr["Products"] = string.Empty;
  
          dr["CostPrice"] = string.Empty;
          dr["SalePrice"] = string.Empty;
          dr["TotalUnits"] = string.Empty;
          dr["TotalCost"] = string.Empty;
  
  
          dt.Rows.Add(dr);
  
          ViewState["CurrentTable"] = dt;
  
          grdSales.DataSource = dt;
          grdSales.DataBind();
          AddNewRow();
          SetRowData();
          if (ViewState["CurrentTable"] != null)
          {
              DataTable dtOld = (DataTable)ViewState["CurrentTable"];
              DataRow drCurrentRow = null;
              int rowIndex = Convert.ToInt32(0);
              if (dtOld.Rows.Count &gt; 1)
              {
                  dtOld.Rows.Remove(dt.Rows[rowIndex]);
                  drCurrentRow = dtOld.NewRow();
                  ViewState["CurrentTable"] = dtOld;
                  grdSales.DataSource = dtOld;
                  grdSales.DataBind();
  
                  for (int i = 0; i &lt; grdSales.Rows.Count - 1; i++)
                  {
                      grdSales.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                  }
                  SetPreviousData();
              }
          }
  
      }
      private 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++)
                  {
  
                      DropDownList ddlProducts =
                        (DropDownList)grdSales.Rows[rowIndex].Cells[1].FindControl("ddlProducts");
  
                      TextBox txtCostPrice =
                        (TextBox)grdSales.Rows[rowIndex].Cells[2].FindControl("txtCp");
                      TextBox txtSalePrice =
                       (TextBox)grdSales.Rows[rowIndex].Cells[3].FindControl("txtSp");
                      TextBox txtUnits =
                       (TextBox)grdSales.Rows[rowIndex].Cells[4].FindControl("txtUnits");
                      TextBox txtTotal =
                       (TextBox)grdSales.Rows[rowIndex].Cells[5].FindControl("txtTotal");
  
                      drCurrentRow = dtCurrentTable.NewRow();
                      drCurrentRow["RowNumber"] = i + 1;
  
  
                      dtCurrentTable.Rows[i - 1]["Products"] = ddlProducts.SelectedValue;
  
                      dtCurrentTable.Rows[i - 1]["CostPrice"] = txtCostPrice.Text;
                      dtCurrentTable.Rows[i - 1]["SalePrice"] = txtSalePrice.Text;
                      dtCurrentTable.Rows[i - 1]["TotalUnits"] = txtUnits.Text;
                      dtCurrentTable.Rows[i - 1]["TotalCost"] = txtTotal.Text;
  
  
                      rowIndex++;
                  }
                  dtCurrentTable.Rows.Add(drCurrentRow);
                  ViewState["CurrentTable"] = dtCurrentTable;
  
                  grdSales.DataSource = dtCurrentTable;
                  grdSales.DataBind();
              }
          }
          else
          {
              Response.Write("ViewState is null");
          }
          SetPreviousData();
      }
      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++)
                  {
  
                      DropDownList ddlProducts =
                        (DropDownList)grdSales.Rows[rowIndex].Cells[1].FindControl("ddlProducts");
  
                      TextBox txtCostPrice = (TextBox)grdSales.Rows[rowIndex].Cells[2].FindControl("txtCp");
                      TextBox txtSalePrice = (TextBox)grdSales.Rows[rowIndex].Cells[3].FindControl("txtSp");
                      TextBox txtUnits = (TextBox)grdSales.Rows[rowIndex].Cells[4].FindControl("txtUnits");
                      TextBox txtTotal = (TextBox)grdSales.Rows[rowIndex].Cells[5].FindControl("txtTotal");
  
  
                      //Added these lines
  
                      ddlProducts.DataValueField = "Key";
                      ddlProducts.DataTextField = "Value";
                      ddlProducts.DataSource = BindProductsDdl();
                      ddlProducts.DataBind();
  
                      //****************
  
                      ddlProducts.SelectedValue = dt.Rows[i]["Products"].ToString();
  
                      txtCostPrice.Text = dt.Rows[i]["CostPrice"].ToString();
                      txtSalePrice.Text = dt.Rows[i]["SalePrice"].ToString();
                      txtUnits.Text = dt.Rows[i]["TotalUnits"].ToString();
                      txtTotal.Text = dt.Rows[i]["TotalCost"].ToString();
                      rowIndex++;
                  }
              }
          }
      }
      private void SetRowData()
      {
          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++)
                  {
  
                      DropDownList ddlProducts =
                         (DropDownList)grdSales.Rows[rowIndex].Cells[1].FindControl("ddlProducts");
  
                      TextBox txtCostPrice = (TextBox)grdSales.Rows[rowIndex].Cells[2].FindControl("txtCp");
                      TextBox txtSalePrice = (TextBox)grdSales.Rows[rowIndex].Cells[3].FindControl("txtSp");
                      TextBox txtUnits = (TextBox)grdSales.Rows[rowIndex].Cells[4].FindControl("txtUnits");
                      TextBox txtTotal = (TextBox)grdSales.Rows[rowIndex].Cells[5].FindControl("txtTotal");
  
                      drCurrentRow = dtCurrentTable.NewRow();
  
                      drCurrentRow["RowNumber"] = i + 1;
  
                      dtCurrentTable.Rows[i - 1]["Products"] = ddlProducts.Text;
  
                      dtCurrentTable.Rows[i - 1]["CostPrice"] = txtCostPrice.Text;
                      dtCurrentTable.Rows[i - 1]["SalePrice"] = txtSalePrice.Text;
                      dtCurrentTable.Rows[i - 1]["TotalUnits"] = txtUnits.Text;
                      dtCurrentTable.Rows[i - 1]["TotalCost"] = txtTotal.Text;
  
                      rowIndex++;
                  }
  
                  ViewState["CurrentTable"] = dtCurrentTable;
                  //grvStudentDetails.DataSource = dtCurrentTable;
                  //grvStudentDetails.DataBind();
              }
          }
          else
          {
              Response.Write("ViewState is null");
          }
          //SetPreviousData();
      }
      private void BindControls()
      {
          BindWarehouseDdl(); BindStaticLists();
  
      }
  
      private Dictionary&lt;int, string&gt; BindProductsDdl()
      {
          Dictionary&lt;int, string&gt; Get = new Dictionary&lt;int, string&gt;();
          for (int i = 0; i &lt; Products.Count; i++)
          {
              int id = Convert.ToInt32(Products[i].id);
              string name = Products[i].Name;
              Get.Add(id, name);
          }
          return Get;
      }
      private void BindStaticLists()
      {
          Classes.CProducts cp = new Classes.CProducts();
          Products = cp.GetAll();
      }
      private void BindWarehouseDdl()
      {
          Dictionary&lt;int, string&gt; Items = new Dictionary&lt;int, string&gt;();
          List&lt;Models.MwareHouse&gt; get = new List&lt;Models.MwareHouse&gt;();
          Classes.CWareHouse cw = new Classes.CWareHouse();
          get = cw.GetAll();
          for (int i = 0; i &lt; get.Count; i++)
          {
              string id = get[i].id;
              string name = get[i].Name;
              Items.Add(Convert.ToInt32(id), name);
  
          }
          ddlWareHouse.DataTextField = "Value";
          ddlWareHouse.DataValueField = "Key";
          ddlWareHouse.DataSource = Items;
          ddlWareHouse.DataBind();
      }
      #endregion
  
      protected void ddlProducts_SelectedIndexChanged(object sender, EventArgs e)
      {
  
      }
  
      protected void grdSales_RowCommand(object sender, GridViewCommandEventArgs e)
      {
          GridViewRow gr = grdSales.SelectedRow;
  
      }
  </code></pre>