GridView 为动态绑定 DataSet,结果 (DataSet)dt = grid1.DataSource 拿不到东西,想偷懒直接添加行不行了, 只好用最笨的办法,把GridView的东西倒成DataSet,然后在DataSet里加行,最后回绑gridview
try里面的
DataTable dtable = new DataTable("tb1");
DataSet ndtset = new DataSet();
for (int i = 0; i < GridView2.HeaderRow.Cells.Count; i++)
{
dtable.Columns.Add(GridView2.HeaderRow.Cells[i].Text.ToString());
}
for (int j = 0; j < GridView2.Rows.Count; j++)
{
DataRow ndr = dtable.NewRow();
for (int c = 0; c < GridView2.Rows[j].Cells.Count; c++)
{
if (GridView2.Rows[j].Cells[c].HasControls())
{
// int kk = GridView2.Rows[j].Cells[c].Controls.Count;
Control tx1 = GridView2.Rows[j].Cells[c].Controls[1]; //默认控件为textbox为例
if (tx1 is TextBox)
{
ndr[c] = ((TextBox)tx1).Text;
}
}
else
ndr[c] = GridView2.Rows[j].Cells[c].Text;
}
dtable.Rows.Add(ndr);
}
dtable.AcceptChanges();
DataRow newRow = dtable.NewRow();
dtable.Rows.Add(newRow);
GridView2.DataSource = dtable;
GridView2.DataBind();
删除一行照样画葫芦,但是需要建立索引
每日喊一遍:VS2008的Gridview烂到掉渣!
本文介绍了一种将ASP.NET GridView中的数据导出到DataSet的方法,并演示了如何在此基础上添加新行和进行更新操作。
&spm=1001.2101.3001.5002&articleId=4878283&d=1&t=3&u=c98497e7bf604a9e927bfd543187a9b0)
1389

被折叠的 条评论
为什么被折叠?



