protected void AdjVouGridview_RowDeleting(object sender, GridViewDeleteEventArgs e) { DataTable dt = (DataTable)ViewState["CurrentTable"]; if (dt.Rows.Count > 1) { dt.Rows.RemoveAt(e.RowIndex); Session["LastQty"] = dt.Rows[dt.Rows.Count - 1]["Column5"]; List <string> PriceList = new List <string>(); foreach (GridViewRow r in AdjVouGridview.Rows) { TextBox tbPrice = (TextBox)r.Cells[3].FindControl("priceTxt"); PriceList.Add(tbPrice.Text); } Session["PriceList"] = PriceList; // ViewState["CurrentTable"] = dt; AdjVouGridview.DataSource = dt; AdjVouGridview.DataBind(); SetPreviousData(); TextBox tbQty = (TextBox)AdjVouGridview.Rows[dt.Rows.Count - 1].Cells[4].FindControl("qtyTxt"); tbQty.Text = (string)Session["LastQty"]; for (int i = 0; i < AdjVouGridview.Rows.Count; i++) { TextBox tbPrice = (TextBox)AdjVouGridview.Rows[i].Cells[3].FindControl("priceTxt"); PriceList = (List <string>)Session["priceList"]; tbPrice.Text = PriceList[i]; } } else { Page.ClientScript.RegisterStartupScript(this.GetType(), "script", "<script>alert('You should choose at least one item!')</script>"); } }
private void AddNewRowToGrid() { if (ViewState["CurrentTable"] != null) { DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"]; DataRow drCurrentRow = null; if (dtCurrentTable.Rows.Count > 0) { drCurrentRow = dtCurrentTable.NewRow(); //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++) { TextBox box1 = (TextBox)AdjVouGridview.Rows[i].Cells[1].FindControl("priceTxt"); TextBox box2 = (TextBox)AdjVouGridview.Rows[i].Cells[2].FindControl("qtyTxt"); dtCurrentTable.Rows[i]["Column4"] = box1.Text; dtCurrentTable.Rows[i]["Column5"] = box2.Text; DropDownList ddl1 = (DropDownList)AdjVouGridview.Rows[i].Cells[0].FindControl("DropDownListCategory"); DropDownList ddl2 = (DropDownList)AdjVouGridview.Rows[i].Cells[1].FindControl("DropDownListItemCode"); DropDownList ddl3 = (DropDownList)AdjVouGridview.Rows[i].Cells[2].FindControl("DropDownListItemName"); dtCurrentTable.Rows[i]["Column1"] = ddl1.SelectedItem.Text; dtCurrentTable.Rows[i]["Column2"] = ddl2.SelectedItem.Text; dtCurrentTable.Rows[i]["Column3"] = ddl2.SelectedItem.Text; } //Rebind the Grid with the current data to reflect changes AdjVouGridview.DataSource = dtCurrentTable; AdjVouGridview.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("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))); dr = dt.NewRow(); dr["Column5"] = string.Empty; dt.Rows.Add(dr); //Store the DataTable in ViewState for future reference ViewState["CurrentTable"] = dt; //Bind the Gridview AdjVouGridview.DataSource = dt; AdjVouGridview.DataBind(); //After binding the gridview, we can then extract and fill the DropDownList with Data DropDownList ddl1 = (DropDownList)AdjVouGridview.Rows[0].Cells[0].FindControl("DropDownListCategory"); TodayDateLbl.Text = DateTime.Today.ToShortDateString(); }