private void AddNewRowToGrid(string text) { int rowIndex = 0; if (ViewState["SuburbTable"] != null) { DataTable dtCurrentTable = (DataTable)ViewState["SuburbTable"]; DataRow drCurrentRow = null; if (dtCurrentTable.Rows.Count > 0 && dtCurrentTable.Rows.Count != 2) { for (int i = 1; i <= dtCurrentTable.Rows.Count; i++) { //extract the TextBox values TextBox box1 = (TextBox)GridViewSuburb.Rows[rowIndex].Cells[0].FindControl("txtSuburb"); box1.Text = text; drCurrentRow = dtCurrentTable.NewRow(); drCurrentRow["Column1"] = box1.Text; rowIndex++; } //add new row to DataTable dtCurrentTable.Rows.Add(drCurrentRow); //Store the current data to ViewState ViewState["SuburbTable"] = dtCurrentTable; //Rebind the Grid with the current data GridViewSuburb.DataSource = dtCurrentTable; GridViewSuburb.DataBind(); } else { lblErrorSuburb.Text = "Maximum number of suburbs you can list is three"; } } else { DataTable dt = new DataTable(); DataRow dr = null; dt.Columns.Add(new DataColumn("Column1", typeof(string))); dr = dt.NewRow(); dr["Column1"] = text; dt.Rows.Add(dr); //Store the DataTable in ViewState ViewState["SuburbTable"] = dt; GridViewSuburb.DataSource = dt; GridViewSuburb.DataBind(); } //Set Previous Data on Postbacks SetPreviousData(); }
private void SetProviderSuburb(string[] secondarySub) { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("Column1", typeof(string))); foreach (var sub in secondarySub) { DataRow dr = dt.NewRow(); dr["Column1"] = sub; dt.Rows.Add(dr); } //Store the DataTable in ViewState GridViewSuburb.DataSource = dt; GridViewSuburb.DataBind(); //Store the DataTable in ViewState ViewState["SuburbTable"] = dt; int rowIndex = 0; if (ViewState["SuburbTable"] != null) { DataTable dtSub = (DataTable)ViewState["SuburbTable"]; if (dt.Rows.Count > 0) { for (int i = 1; i < dtSub.Rows.Count; i++) { foreach (var Sub in secondarySub) { TextBox box1 = (TextBox)GridViewSuburb.Rows[rowIndex].Cells[0].FindControl("txtSuburb"); box1.Text = Sub; rowIndex++; //Store the DataTable in ViewState } } } } }