Click here to Skip to main content
15,909,614 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am developing dynamic table with dropdown list when i click on add button it will show one dynamic table with Add button for creating another dynamic table i will fill data and stored in database

look on my code below



private void AddNewRowToGrid()
   {
       int rowIndex = 0;
       if (ViewState["CurrentTable"] != null)
       {
           DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
           DataRow drCurrentRow = null;
           if (Gridview1.Rows.Count > 0)
           {
               int intRow = Gridview1.Rows.Count;
               DropDownList val1 = (DropDownList)Gridview1.Rows[intRow - 1].Cells[1].FindControl("txtItemCode");


               TextBox val2 = (TextBox)Gridview1.Rows[intRow - 1].Cells[2].FindControl("txtDescription");
               TextBox val3 = (TextBox)Gridview1.Rows[intRow - 1].Cells[3].FindControl("txtUOM");
               TextBox val4 = (TextBox)Gridview1.Rows[intRow - 1].Cells[4].FindControl("txtRequestQty");
           //  TextBox val5 = (TextBox)Gridview1.Rows[intRow - 1].Cells[5].FindControl("txtAmount");

                   for (int i = 0; i < Gridview1.Rows.Count; i++)
                   {
                       //extract the TextBox values
                       //Label lblSno = (Label)Gridview1.Rows[rowIndex].Cells[0].FindControl("lblSno");
                       DropDownList box1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("txtItemCode");
                       TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txtDescription");
                       TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("txtUOM");
                       TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("txtRequestQty");
                  //  TextBox box5= (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("txtAmount");

                       //dtCurrentTable.Rows[i][strSno] = lblSno.Text;
                       dtCurrentTable.Rows[i][ItemCode] = box1.Text;
                       dtCurrentTable.Rows[i][strDescription] = box2.Text;
                       dtCurrentTable.Rows[i][UOM] = box3.Text;
                       dtCurrentTable.Rows[i][RequestQty] = box4.Text;
                     //  dtCurrentTable.Rows[i][Amount] = (Convert.ToInt32(box3.Text.ToString().Trim()) * Convert.ToInt32(box4.Text.ToString().Trim()));

                       rowIndex++;
                   }
                   drCurrentRow = dtCurrentTable.NewRow();

                   //add new row to DataTable
                   dtCurrentTable.Rows.Add(drCurrentRow);

                   for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                   {
                       // DataRow dr = null;
                       //dr[strSno] = i;
                       dtCurrentTable.Rows[i - 1][strSno] = i;
                   }
                   dtCurrentTable.AcceptChanges();
                   //Store the current data to ViewState
                   ViewState["CurrentTable"] = dtCurrentTable;
                   //Rebind the Grid with the current data
                   Gridview1.DataSource = dtCurrentTable;
                   Gridview1.DataBind();
           }
       }
       else
       {
           Response.Write("ViewState is null");
       }
   }
   protected void ButtonAdd_Click(object sender, EventArgs e)
   {
       AddNewRowToGrid();
   }


99% of my code is fine but when I click on add button for creating new dynamic table the first table dropdown selected item changed to first list item how to maintain view state?

[Edit]Code is wrapped in "pre" tags[/Edit]
Posted
Updated 3-Jan-11 21:49pm
v7

1 solution

Why so much of code man? Only AddNewRowToGrid() would have been fine.

I didn't check the full code but found something wrong in a quick look.

Your DataRow object - AddNewRowToGrid is null and never assigned the values of the last row i.e the values from the objects val2, val3, val4 and val5.

Check the method AddNewRowToGrid() again.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900