这个肯定很傻,但我找不到解决我的问题的方法。我想要做的是让用户按需将行添加到加载为空的网格视图中。但是每次我按下“添加”按钮时,我都会松开前一行。
我确信这非常简单,包括使用不合适的控件。
这是我的代码隐藏:
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Binding();
}
}
protected void Binding()
{
if (dt == null)
{
dt = new DataTable();
dt.Columns.Add("txt1");
dt.Columns.Add("txt2");
DataRow dr = dt.NewRow();
dr[0] = "Text1";
dr[1] = "Text2";
dt.Rows.Add(dr);
dt.AcceptChanges();
}
grd.DataSource = dt;
grd.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable tt = grd.DataSource as DataTable;
//Both dt and grd.DataSource are null at this point
if (tt != null)
{
DataRow dr = tt.NewRow();
dr[0] = "Text1";
dr[1] = "Text2";
tt.Rows.Add(dr);
tt.AcceptChanges();
grd.DataSource = tt;
grd.DataBind();
}
}
非常感谢您的帮助!