简体   繁体   中英

Add row to unbound DataGridView in asp.net

I want to add a row to UnBounded DataGridView in Asp.net. I can create rows like this in Winform but don't know how to add row in ASPX as it don't have GridView.Rows.Add() method.

This is my code

Datatable declare at page load event.

dt.Columns.Add("Account_Code", Type.GetType("System.String"));
dt.Columns.Add("Account_Name", Type.GetType("System.String"));

dt.Rows.Add();
int rowCount=dt.Rows.Count-1;
dt.Rows[rowCount]["Account_Code"] = txtAccountCode.Text;
dt.Rows[rowCount]["Account_Name"] = txtAccountName.Text;
gvVoucherDetail.DataSource = dt;
gvVoucherDetail.DataBind();

When I see the datatable in the debuging mode it shows me row added to DataTable. But not sure why it is not showing in the web page. Any idea?

you will need to save the datatable in a session variable to maintain the state during postbacks. Here's a link tha I used to get it to work.

http://www.tek-tips.com/viewthread.cfm?qid=1282063

Because when you bind it, it clears all existing rows and injects its new rows. If you want the manually added rows to appear after a DataBind call, you have to add them either after the RowDataBound events have completed or during that process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM