简体   繁体   English

将行添加到asp.net中的未绑定DataGridView

[英]Add row to unbound DataGridView in asp.net

I want to add a row to UnBounded DataGridView in Asp.net. 我想在Asp.net中向UnBounded DataGridView添加一行。 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. 我可以在Winform中创建这样的行,但是不知道如何在ASPX中添加行,因为它没有GridView.Rows.Add()方法。

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. 当我在调试模式下看到数据表时,它向我显示了添加到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 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. 如果希望手动添加的行出现在DataBind调用之后,则必须在RowDataBound事件完成后或在此过程中添加它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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