asp.net - Add New Rows to GridView -


i have gridview on page , on button_onclick event i'm wanting add new row grid.

i can add row table using following code, works fine, i'm binding datatable grid previous entries lost.

        string selectedproduct= ddlproducts.selecteditem.text;         datatable datatable = new datatable();          datatable.columns.add("product");         datarow datarow;          datarow = datatable.newrow();         datarow["product"] = selectedproduct;         datatable.rows.add(datarow);          grdselectedproducts.datasource = datatable;         grdselectedproducts.databind(); 

so whilst understand why causing data loss, i'm not sure how around it.

how can add new row gridview on each button click whilst retaining added row? data not stored anywhere other grid itself, there no real datasource.

there options such add row gridview on client side uses jquery, have read isn't great idea use when adding / removing items grid. perhaps wrong? or there add new row gridview there isn't detail there.

you need store products viewstate (or sessionstate or database) can persist on post back.

for example,

private datatable productdatatable {     { return viewstate["productdatatable"] datatable ?? new datatable(); }     set { viewstate["productdatatable"] = value; } }  protected void addrowbutton_click(object sender, eventargs e) {     string selectedproduct = ddlproducts.selecteditem.text;      // data viewstate     datatable datatable = productdatatable;      datatable.columns.add("product");     datarow datarow;      datarow = datatable.newrow();     datarow["product"] = selectedproduct;     datatable.rows.add(datarow);      // save data viewstate     productdatatable = datatable;      grdselectedproducts.datasource = datatable;     grdselectedproducts.databind(); } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -