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?