在页面中插入一个DataGrid,并且添加“可用列”中的“按钮列--(编辑,更新,取消)”。绑定的数据源是测试数据库Northwind中的表Catagories。
编译后进行测试,点击编辑后出现TextBox,更改内容后点“更新”按钮提示如下错误,请问这是为什么?
应用程序中的服务器错误。
--------------------------------------------------------------------------------索引超出范围。必须为非负值并小于集合大小。参数名: index
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。参数名: index源错误:
行 142:
行 143: // Gets the value of the key field of the row being updated
行 144: string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
行 145:
行 146: // Gets get the value of the controls (textboxes) that the user
源代码:
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
sqlDataAdapter1.Fill(dataSet41);
if (!IsPostBack)
{
DataGrid1.DataBind();
} } private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
} private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();
} private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string categoryName, categoryDescription;
string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
TextBox tb;
tb = (TextBox)(e.Item.Cells[2].Controls[0]);
categoryName = tb.Text;
tb = (TextBox)(e.Item.Cells[3].Controls[0]);
categoryDescription = tb.Text;
DataSet4.CategoriesRow r;
r = dataSet41.Categories.FindByCategoryID(int.Parse(key));
r.CategoryName = categoryName;
r.Description = categoryDescription;
sqlDataAdapter1.Update(dataSet41);
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();
}
}
编译后进行测试,点击编辑后出现TextBox,更改内容后点“更新”按钮提示如下错误,请问这是为什么?
应用程序中的服务器错误。
--------------------------------------------------------------------------------索引超出范围。必须为非负值并小于集合大小。参数名: index
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。参数名: index源错误:
行 142:
行 143: // Gets the value of the key field of the row being updated
行 144: string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
行 145:
行 146: // Gets get the value of the controls (textboxes) that the user
源代码:
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
sqlDataAdapter1.Fill(dataSet41);
if (!IsPostBack)
{
DataGrid1.DataBind();
} } private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
} private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();
} private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string categoryName, categoryDescription;
string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
TextBox tb;
tb = (TextBox)(e.Item.Cells[2].Controls[0]);
categoryName = tb.Text;
tb = (TextBox)(e.Item.Cells[3].Controls[0]);
categoryDescription = tb.Text;
DataSet4.CategoriesRow r;
r = dataSet41.Categories.FindByCategoryID(int.Parse(key));
r.CategoryName = categoryName;
r.Description = categoryDescription;
sqlDataAdapter1.Update(dataSet41);
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();
}
}
可以看出,这样的错误是在给DataGrid从新绑定时出的错,可能是
你已经点击修改了,但是没确定修改,而是又改变条件查询了,查询所的的
DataSet的记录个数小于你原来的DataSet的记录个数,在查询的时候,或翻页的时候
一定要注意。
// Gets the value of the key field of the row being updated
行 144: string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
行 145:
行 146: // Gets get the value of the controls (textboxes) that the user
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();你也没指定数据员DataSource,就这么绑定那么DataGrid1中是0行啊。
而e.Item.ItemIndex的值可不是0啊!!
而且在Page_load的时候执行:
sqlDataAdapter1.Fill(dataSet41);
if (!IsPostBack)
{
DataGrid1.DataBind();
}
http://msdn.microsoft.com/library/chs/default.asp?url=/library/chs/vbcon/html/vbwlkwalkthroughdisplayingdatainlistboxesonwebformspage.asp
categoryName = tb.Text;
tb = (TextBox)(e.Item.Cells[3].Controls[0]);
把这个地方改一下,有几列减1就行了,因为是从0开始!
FillGrid(cmdNext)。
private void FillGrid(System.Data.SqlClient.SqlCommand currentSqlCommand)
{
System.Data.SqlClient.SqlDataReader dr;
sqlConnection1.Open();
dr = currentSqlCommand.ExecuteReader();
DataGrid1.DataSource = dr;
DataGrid1.DataBind();
dr.Close();
sqlConnection1.Close();
ViewState["CurrentPage"] = CurrentPage;
ViewState[CurrentPage.ToString()] = DataGrid1.Items[0].Cells[0].Text;
// Determine how many rows were filled into the grid. If it is less
// than the number of rows per page, there are no more rows in the
// table, and the Next button should be disabled.
if (DataGrid1.Items.Count < DataGrid1.PageSize)
{
btnNext.Enabled = false;
}
}而不是简单的DataGrid1.DataBind();
按照MSDN给出了例子,DataGrid1确实只是执行DataGrid1.DataBind()实现绑定呀。
而且并没有什么复杂操作,只是选择库中三列数据:SELECT CategoryID, CategoryName, Description
FROM Categories