public partial class Admin_Invoicesl : UserControlDataAccess
{
DataTable productsDt = new DataTable();
DataTable GvDT=new DataTable();
DataRow GVr = null;
protected void Page_Load(object sender, EventArgs e)
{
productsDt = DA.GetProducts(null, null, null, null, "2");
if (!IsPostBack)
{
SetInitialRow();
}
}
private void SetInitialRow()
{
GvDT.Columns.Add(new DataColumn("RowNumber", typeof(string)));
GvDT.Columns.Add(new DataColumn("Product", typeof(string)));
GvDT.Columns.Add(new DataColumn("Qty", typeof(string)));
GVr = GvDT.NewRow();
GVr["RowNumber"] = 1;
GVr["Product"] = string.Empty;
GVr["Qty"] = string.Empty;
GvDT.Rows.Add(GVr);
Session["DT"] = GvDT;
Session.Timeout = 60;
Gridview1.DataSource = (DataTable)Session["DT"];
Gridview1.DataBind();
}
private void AddNewRowToGrid()
{
setprevData();
DataTable dt = (DataTable)Session["DT"];
GVr = dt.NewRow();
GVr["RowNumber"] = dt.Rows.Count+1;
GVr["Product"] = string.Empty;
GVr["Qty"] = string.Empty;
dt.Rows.Add(GVr);
Session["DT"] = dt;
}
protected void setprevData()
{
DataTable prevdata = (DataTable)Session["DT"];
for (int i = 0; i < Gridview1.Rows.Count; i++)
{
prevdata.Rows[i]["Product"] = ((DropDownList)Gridview1.Rows[i].FindControl("DProducts")).SelectedValue.ToString();
prevdata.Rows[i]["Qty"] = ((TextBox)Gridview1.Rows[i].FindControl("T_Quantity")).Text;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
Gridview1.DataSource = (DataTable)Session["DT"];
Gridview1.DataBind();
}