I have problems when creating new rows.I have a GridView 3 columns:
DropDownList (column name: ProductName), textbox (name column: Number), TextBox (column name: value) and Button Field to add new row with CommandName="addrow"
when the page loads,I bind Data into DropDownList.
The user will select the product name in DropDownList and enter the value into the textbox in the column named price,quantity.
After clicking LinkButtonField,a new row is added to the GridView.
But the value of previous row not stored.
Because for loop re-create controls of previous row(Dropdownlist,TextBox.
How to value of previous row not lost?
My code:
clsProduct p=new clsProduct();
DataTable dt;
void LoadData()
{
dt =
GridView2.DataSource = tbl;
GridView2.DataBind();
for (int i= 0; i < GridView2.Rows.Count; i++)
{
DropDownList cbo = (DropDownList)GridView2.Rows[cborow].Cells[0].FindControl("DrdProName");
cbo.DataSource = dt;
cbo.DataValueField = "idPro";
cbo.DataTextField = "TProName";
cbo.DataBind();
}
Make table for GridView:
void MakeTableGridView()
{
if (tbl.Rows.Count == 0 || tbl==null)
{
tbl.Columns.Add("idPro", typeof(string));
tbl.Columns.Add("quantity", typeof(int));
tbl.Columns.Add("price", typeof(int));
RowNew();
}
}
New row:
void RowNew()
{
dr = tbl.NewRow();
dr[0] = "";
dr[1] = "1";
dr[2] = "0";
tbl.AcceptChanges();
tbl.Rows.Add(dr);
}
Event when click AddRow LinkButtonField:
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
string cmd = e.CommandName;
if (cmd =="addrow")
{
RowNew();
LoadData();
}
}