指定的参数已超出有效值的范围。参数名: index
GXUN 2006-02-05 11:12:46 在CSDN上搜索到同样的问题有3个,跟帖的人不少,但是没有能给给出解决方法,在此再帖出来让大家讨论一下怎么解决.
在做DataGrid的编辑时遇到的问题,点击"更新"就弹出这个错误:
异常详细信息: System.ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。参数名: index
源错误:
行 145: string ID=e.Item.Cells[0].Text.ToString();
行 146: Label1.Text=((TextBox)e.Item.Cells[1].Controls[0]).Text.ToString();
//下面两行都是正确的,就是上面这行(行 146:)有问题
//Label1.Text=((TextBox)e.Item.Cells[0].Controls[0]).Text.ToString();
//Label1.Text=((TextBox)e.Item.Cells[2].Controls[0]).Text.ToString();
源文件: e:\asp.net应用程序\cyt\admin_searchfile.aspx.cs 行: 146
我的代码如下(点击"更新"时触发):
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string dsn=ConfigurationSettings.AppSettings["DSN"];
SqlConnection Conn=new SqlConnection(dsn);
Conn.Open();
//获取相应行的相应字段值
string ID=e.Item.Cells[0].Text.ToString();
Label1.Text=((TextBox)e.Item.Cells[1].Controls[0]).Text.ToString();
/*
string number=((TextBox)e.Item.Cells[1].Controls[0]).Text.ToString();
string name=((TextBox)e.Item.Cells[2].Controls[0]).Text.ToString();
string page=((TextBox)e.Item.Cells[3].Controls[0]).Text.ToString();
string time=((TextBox)e.Item.Cells[4].Controls[0]).Text.ToString();
//更新数据
string Sql="update text set number='"+number+"',text='"+name+"',page_num='"+page+"',release_time='"+time+" where id="+ID;
SqlCommand Comm=new SqlCommand(Sql,Conn);
Comm.ExecuteNonQuery();
DataGrid1.EditItemIndex=-1; //禁止用户编辑DataGrid
BindGrid(); //重新填充
*/
Conn.Close();
}