protected void gd_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtAuthor;
TextBox txtBook;
dt = new DataTable();
dt=(DataTable)ViewState["dt"];
GridViewRow gdr = gd.Rows[e.RowIndex];
if (gdr != null)
{
txtAuthor = (TextBox)gd.Rows[e.RowIndex].FindControl("txtAuthor1");
txtBook = (TextBox)gd.Rows[e.RowIndex].FindControl("txtBook1");
dt = (DataTable)ViewState["dt"];
string txtAuthorName;
string txtBookName;
if (txtAuthor != null && txtBook!=null)
{
txtAuthorName = txtAuthor.Text;
txtBookName = txtBook.Text;
int i=0;
dt = (DataTable)Session["dt"];
for (; i < gd.Rows.Count; i++)
{
if (e.RowIndex == i)
{
dt.Rows[i][0] = txtAuthor.Text;
dt.Rows[i][1] = txtBook.Text;
ViewState["dt"] = dt;
gd.DataSource = dt;
gd.DataBind();
}
}
}
}
dt = (DataTable)ViewState["dt"];
gd.DataSource = dt;
gd.DataBind();
}
问问题
620 次
1 回答
1
该错误可能是因为试图访问一行不存在的数据表。要将值分配给datatable
行,您必须检查是否有该行,datatable
您正在检查行grid
而不是datatable
您不需要循环,因为您可以直接访问datatable
具有索引的行,只需检查是否e.RowIndex
是有效的行索引datatable
。
if (e.RowIndex <= dt.Rows.Count)
{
dt.Rows[e.RowIndex][0] = txtAuthor.Text;
dt.Rows[e.RowIndex][1] = txtBook.Text;
ViewState["dt"] = dt;
gd.DataSource = dt;
gd.DataBind();
}
于 2013-04-13T11:59:05.253 回答