
dt.Columns.Add("col2", typeof(string))
dt.Columns.Add("col3", typeof(string))
dt.Rows.Add(new object[] { col1, col2, col3 })
这个连列也建了,按需修改吧
//1)从数据集中取出一个已有的表DataTable tbl = ds.Tables[0]
//2)创建新行row
DataRow row = tbl.NewRow()
//3)对row各个字段赋值
row[0] = "某某某"
row[1] = ……
……
//4)将新建的行加入到表中
tbl.Rows.Add(row)
问题可能出在 dt_stockIn 上,你的 dt_stockIn 是个全局变量吗 ?你在btn_addOne_Click事件中对进行新增行 *** 作后,是如何保存它的 ?
要知道btn_addOne_Click事件后页面会刷新,如果你没有对dt_stockIn进行保存,那么它的值会丢失。建议用一个session保存一下。
以下是我的测试代码:
protected void Page_Load( object sender, EventArgs e )
{
if( IsPostBack == false )
{
DataTable dt_stockIn = ds.Tables["UserInfo"]
GridView1.DataSource = dt_stockIn
GridView1.DataBind()
Session["table"] = ds.Tables["UserInfo"]
}
}
protected void Button1_Click( object sender, EventArgs e )
{
DataTable dt_stockIn = ( DataTable )Session["table"]
dt_stockIn.Rows.Add( dt_stockIn.NewRow() )
GridView1.DataSource = dt_stockIn
GridView1.DataBind()
}
以上代码仅供参考。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)