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烂到掉渣!