title: Dropdownlist loses selection on adding dynamic row in gridview
body: <p>I have this code below. I am trying to add rows in gridview on demand and then bind the dropdownlist in the gridview and on adding a new row want to retain the values selected in the previous row. The problem is i am able to bind the dropdownlist and also do a cascading, but on ading a new row i am not able to retain the selection of the previous row. Please review the code and let me know where i am wrong.</p>
  
  <pre><code>&lt;asp:GridView ID="gvAdvisor" runat="server" ShowFooter="true" AutoGenerateColumns="false" Width="895px" OnRowDataBound="gvAdvisor_RowDataBound"&gt;
                                  &lt;Columns&gt;
                                      &lt;asp:BoundField DataField="RowNumber" HeaderText="Row Number" /&gt;
                                      &lt;asp:TemplateField HeaderText="Region"&gt;
                                          &lt;ItemTemplate&gt;
                                              &lt;asp:DropDownList ID="ddRegion" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddRegion_SelectedIndexChanged"&gt;
                                              &lt;/asp:DropDownList&gt;
                                          &lt;/ItemTemplate&gt;
                                      &lt;/asp:TemplateField&gt;
                                      &lt;asp:TemplateField HeaderText="Agency"&gt;
                                          &lt;ItemTemplate&gt;
                                              &lt;asp:DropDownList ID="ddAgency" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddAgency_SelectedIndexChanged"&gt;
                                              &lt;/asp:DropDownList&gt;
                                          &lt;/ItemTemplate&gt;
                                      &lt;/asp:TemplateField&gt;
                                      &lt;asp:TemplateField HeaderText="Firm"&gt;
                                          &lt;ItemTemplate&gt;
                                              &lt;asp:DropDownList ID="ddFirm" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddFirm_SelectedIndexChanged"&gt;
                                              &lt;/asp:DropDownList&gt;
                                          &lt;/ItemTemplate&gt;
                                      &lt;/asp:TemplateField&gt;
                                      &lt;asp:TemplateField HeaderText="Office"&gt;
                                          &lt;ItemTemplate&gt;
                                              &lt;asp:DropDownList ID="ddOffice" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddOffice_SelectedIndexChanged"&gt;
                                              &lt;/asp:DropDownList&gt;
                                          &lt;/ItemTemplate&gt;
                                      &lt;/asp:TemplateField&gt;
                                      &lt;asp:TemplateField HeaderText="Advisor"&gt;
                                          &lt;ItemTemplate&gt;
                                              &lt;asp:DropDownList ID="ddAdvisor" runat="server"&gt;
                                              &lt;/asp:DropDownList&gt;
                                          &lt;/ItemTemplate&gt;
                                      &lt;/asp:TemplateField&gt;
                                      &lt;asp:TemplateField HeaderText="%"&gt;
                                          &lt;ItemTemplate&gt;
                                              &lt;asp:TextBox ID="txtPercent" runat="server" /&gt;
                                          &lt;/ItemTemplate&gt;
                                          &lt;FooterStyle HorizontalAlign="Right" /&gt;
                                          &lt;FooterTemplate&gt;
                                              &lt;asp:Button ID="AddRowButton" runat="server" Text="Add New Row"
                                                  OnClick="ButtonAdd_Click" /&gt;
                                          &lt;/FooterTemplate&gt;
                                      &lt;/asp:TemplateField&gt;
                                  &lt;/Columns&gt;
                              &lt;/asp:GridView&gt;
  
  
  
  
  
          protected void Page_Load(object sender, EventArgs e)
              {
                  if (!IsPostBack)
                  {
                      SetInitialRow();
                  }
              }
  
  
          private void SetInitialRow()
              {
                  try
                  {
                      DataTable dt = new DataTable();
                      DataRow dr = null;
                      dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
                      dt.Columns.Add(new DataColumn("Column1", typeof(string)));
                      dt.Columns.Add(new DataColumn("Column2", typeof(string)));
                      dt.Columns.Add(new DataColumn("Column3", typeof(string)));
                      dt.Columns.Add(new DataColumn("Column4", typeof(string)));
                      dt.Columns.Add(new DataColumn("Column5", typeof(string)));
                      dt.Columns.Add(new DataColumn("Column6", typeof(string)));
  
  
                      dr = dt.NewRow();
                      dr["RowNumber"] = 1;
                      dr["Column1"] = string.Empty;
                      dr["Column2"] = string.Empty;
                      dr["Column3"] = string.Empty;
                      dr["Column4"] = string.Empty;
                      dr["Column5"] = string.Empty;
                      dr["Column6"] = string.Empty;
  
                      dt.Rows.Add(dr);
  
                      //Store the DataTable in ViewState
                      ViewState["CurrentTable"] = dt;
  
                      gvAdvisor.DataSource = dt;
                      gvAdvisor.DataBind();
                  }
                  catch (Exception Ex)
                  { 
                      //logger.Info("SetInitialRow : " + Ex.Message); 
                  }
              }
  
      private void AddNewRowToGrid()
          {
              try
              {
                  int rowIndex = 0;
                  if (ViewState["CurrentTable"] != null)
                  {
                      DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                      DataRow drCurrentRow = null;
                      if (dtCurrentTable.Rows.Count &gt; 0)
                      {
                          //extract the TextBox values
  
                          //if (tbBushels.Text != "" &amp;&amp; tbLocation.Text != "" &amp;&amp; tbFuture.Text != "" &amp;&amp; tbBasis.Text != "" &amp;&amp; tbPrice.Text != "" &amp;&amp; tbRevenue.Text != "")
                          //{
                          for (int i = 1; i &lt;= dtCurrentTable.Rows.Count; i++)
                          {
                              DropDownList Region =
                                  (DropDownList)gvAdvisor.Rows[rowIndex].Cells[1].FindControl("ddRegion");
                              DropDownList Agency =
                                  (DropDownList)gvAdvisor.Rows[rowIndex].Cells[2].FindControl("ddAgency");
                              DropDownList Firm =
                                  (DropDownList)gvAdvisor.Rows[rowIndex].Cells[3].FindControl("ddFirm");
                              DropDownList Office =
                                  (DropDownList)gvAdvisor.Rows[rowIndex].Cells[4].FindControl("ddOffice");
                              DropDownList Advisor =
                                  (DropDownList)gvAdvisor.Rows[rowIndex].Cells[5].FindControl("ddAdvisor");
                              TextBox Percent =
                                  (TextBox)gvAdvisor.Rows[rowIndex].Cells[6].FindControl("txtPercent");
  
  
                              drCurrentRow = dtCurrentTable.NewRow();
                              drCurrentRow["RowNumber"] = i + 1;                        
                              drCurrentRow["Column1"] = Region.SelectedItem.Value;
                              drCurrentRow["Column2"] = Agency.SelectedItem.Value;
                              drCurrentRow["Column3"] = Firm.SelectedItem.Value;
                              drCurrentRow["Column4"] = Office.SelectedItem.Value;
                              drCurrentRow["Column5"] = Advisor.SelectedItem.Value;
                              drCurrentRow["Column6"] = Percent.Text;
  
                              rowIndex++;
                          }
                          //add new row to DataTable
                          dtCurrentTable.Rows.Add(drCurrentRow);
                          //Store the current data to ViewState
                          ViewState["CurrentTable"] = dtCurrentTable;
  
                          //Rebind the Grid with the current data
                          gvAdvisor.DataSource = dtCurrentTable;
                          gvAdvisor.DataBind();
  
                          //}
                      }
  
                  }
                  else
                  {
                      Response.Write("ViewState is null");
                  }
  
                  //Set Previous Data on Postbacks
                  SetPreviousData();
              }
              catch (Exception Ex)
              { 
                  //logger.Info("AddNewRowToGrid : " + Ex.Message);
              }
          }
  
          private void SetPreviousData()
          {
              try
              {
                  int rowIndex = 0;
                  if (ViewState["CurrentTable"] != null)
                  {
                      DataTable dt = (DataTable)ViewState["CurrentTable"];
                      if (dt.Rows.Count &gt; 0)
                      {
                          for (int i = 1; i &lt; dt.Rows.Count; i++)
                          {                        
                              DropDownList Region =
                                  (DropDownList)gvAdvisor.Rows[rowIndex].Cells[1].FindControl("ddRegion");
                              Region.SelectedItem.Value = dt.Rows[i]["Column1"].ToString();
                              foreach (GridViewRow gvr in gvAdvisor.Rows)
                              {
                                  if (gvr.RowType == DataControlRowType.DataRow)
                                  {
                                     // DropDownList ddRegion = (DropDownList)gvr.FindControl("ddRegion");
                                      DropDownList ddAgency = (DropDownList)gvr.FindControl("ddAgency");
  
                                      if (Region.SelectedItem.Value == "0")
                                      {
  
                                      }
                                      else
                                      {
                                          ddHelp.BindAgencyByRegion(ddAgency, Convert.ToInt32(Region.SelectedItem.Value));
                                      }
                                      ListItem ll1 = new ListItem("Select", "0");
                                      ddAgency.Items.Insert(0, ll1);
                                  }
                              }
  
                              DropDownList Agency =
                                  (DropDownList)gvAdvisor.Rows[rowIndex].Cells[2].FindControl("ddAgency");
                              Agency.SelectedItem.Value = dt.Rows[i]["Column2"].ToString();
                              foreach (GridViewRow gvr in gvAdvisor.Rows)
                              {
                                  if (gvr.RowType == DataControlRowType.DataRow)
                                  {
                                      //DropDownList ddAgency = (DropDownList)gvr.FindControl("ddAgency");
                                      DropDownList ddFirm = (DropDownList)gvr.FindControl("ddFirm");
  
                                      if (Agency.SelectedItem.Value == "0")
                                      {
  
                                      }
                                      else
                                      {
                                          ddHelp.BindFirm(ddFirm, Convert.ToInt32(Agency.SelectedItem.Value));
                                      }
                                      ListItem ll2 = new ListItem("Select", "0");
                                      ddFirm.Items.Insert(0, ll2);
                                  }
                              }
  
                              DropDownList Firm =
                                  (DropDownList)gvAdvisor.Rows[rowIndex].Cells[3].FindControl("ddFirm");
                              Firm.SelectedItem.Value = dt.Rows[i]["Column3"].ToString();
                              foreach (GridViewRow gvr in gvAdvisor.Rows)
                              {
                                  if (gvr.RowType == DataControlRowType.DataRow)
                                  {
                                      //DropDownList ddFirm = (DropDownList)gvr.FindControl("ddFirm");
                                      DropDownList ddOffice = (DropDownList)gvr.FindControl("ddOffice");
  
                                      if (Firm.SelectedItem.Value == "0")
                                      {
  
                                      }
                                      else
                                      {
                                          ddHelp.BindOffice(ddOffice, 0, Convert.ToInt32(Firm.SelectedItem.Value), 0);
                                      }
                                      ListItem ll3 = new ListItem("Select", "0");
                                      ddOffice.Items.Insert(0, ll3);
                                  }
                              }
                              DropDownList Office =
                                  (DropDownList)gvAdvisor.Rows[rowIndex].Cells[4].FindControl("ddOffice");
                              Office.SelectedItem.Value = dt.Rows[i]["Column4"].ToString();
                              foreach (GridViewRow gvr in gvAdvisor.Rows)
                              {
                                  if (gvr.RowType == DataControlRowType.DataRow)
                                  {
                                      //DropDownList ddOffice = (DropDownList)gvr.FindControl("ddOffice");
                                      DropDownList ddAdvisor = (DropDownList)gvr.FindControl("ddAdvisor");
  
                                      if (Office.SelectedItem.Value == "0")
                                      {
  
                                      }
                                      else
                                      {
                                          ddHelp.BindAdvisor(ddAdvisor, 0, 0, Convert.ToInt32(Office.SelectedItem.Value));
                                      }
                                      ListItem ll4 = new ListItem("Select", "0");
                                      ddAdvisor.Items.Insert(0, ll4);
                                  }
                              }
                              DropDownList Advisor = 
                                  (DropDownList)gvAdvisor.Rows[rowIndex].Cells[5].FindControl("ddAdvisor");
                              TextBox Percent =
                                  (TextBox)gvAdvisor.Rows[rowIndex].Cells[6].FindControl("txtPercent");
  
  
                              Advisor.SelectedItem.Value = dt.Rows[i]["Column5"].ToString();
                              Percent.Text = dt.Rows[i]["Column6"].ToString();
  
                              rowIndex++;
                          }
                      }
                  }
              }
              catch (Exception Ex)
              { 
                  //logger.Info("SetPreviousData : " + Ex.Message); 
              }
          }
  
  protected void ButtonAdd_Click(object sender, EventArgs e)
      {
          AddNewRowToGrid();
  
      }
  </code></pre>