private void AddNewRowToGrid() { if (ViewState["CurrentTable"] != null) { DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"]; DataRow drCurrentRow = null; if (dtCurrentTable.Rows.Count > 0) { drCurrentRow = dtCurrentTable.NewRow(); drCurrentRow["RowNumber"] = dtCurrentTable.Rows.Count + 1; //add new row to DataTable dtCurrentTable.Rows.Add(drCurrentRow); //Store the current data to ViewState for future reference ViewState["CurrentTable"] = dtCurrentTable; for (int i = 0; i < dtCurrentTable.Rows.Count - 1; i++) { //extract the TextBox values TextBox box1 = (TextBox)GridV1.Rows[i].Cells[2].FindControl("TextBox1"); TextBox box2 = (TextBox)GridV1.Rows[i].Cells[3].FindControl("TextBox2"); TextBox box3 = (TextBox)GridV1.Rows[i].Cells[4].FindControl("TextBox3"); TextBox box4 = (TextBox)GridV1.Rows[i].Cells[5].FindControl("TextBox4"); TextBox box5 = (TextBox)GridV1.Rows[i].Cells[6].FindControl("TextBox5"); dtCurrentTable.Rows[i]["Column2"] = box1.Text; dtCurrentTable.Rows[i]["Column3"] = box2.Text; dtCurrentTable.Rows[i]["Column4"] = box3.Text; dtCurrentTable.Rows[i]["Column5"] = box4.Text; dtCurrentTable.Rows[i]["Column6"] = box5.Text; //extract the DropDownList Selected Items DropDownList ddl1 = (DropDownList)GridV1.Rows[i].Cells[1].FindControl("DropDownList1"); // Label lbl1 = (Label)Gridview1.Rows[i].Cells[3].FindControl("Label1"); //DropDownList ddl2 = (DropDownList)Gridview1.Rows[i].Cells[2].FindControl("DropDownList2"); // Update the DataRow with the DDL Selected Items dtCurrentTable.Rows[i]["Column1"] = ddl1.SelectedItem.Text; //dtCurrentTable.Rows[i]["Column3"] = lbl1.Text; //dtCurrentTable.Rows[i]["Column2"] = ddl2.SelectedItem.Text; } //Rebind the Grid with the current data to reflect changes GridV1.DataSource = dtCurrentTable; GridV1.DataBind(); } } else { Response.Write("ViewState is null"); } //Set Previous Data on Postbacks SetPreviousData(); }
private void SetInitialRow() { 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))); //for TextBox value dt.Columns.Add(new DataColumn("Column3", typeof(string))); //for TextBox value dt.Columns.Add(new DataColumn("Column4", typeof(string))); //for TextBox value dt.Columns.Add(new DataColumn("Column5", typeof(string))); //for TextBox value dt.Columns.Add(new DataColumn("Column6", typeof(string))); //for TextBox value dr = dt.NewRow(); dr["RowNumber"] = 1; 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 for future reference ViewState["CurrentTable"] = dt; //Bind the Gridview GridV1.DataSource = dt; GridV1.DataBind(); //After binding the gridview, we can then extract and fill the DropDownList with Data DropDownList ddl1 = (DropDownList)GridV1.Rows[0].Cells[1].FindControl("DropDownList1"); //Label lbl1 = (Label)Gridview1.Rows[0].Cells[3].FindControl("Label1"); // DropDownList ddl2 = (DropDownList)Gridview1.Rows[0].Cells[2].FindControl("DropDownList2"); // FillDropDownList(ddl1); // FillDropDownList(ddl2); }