指定的参数已超出有效值的范围。参数名: index

GXUN GXUN 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
在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();
}
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
GXUN
.NET社区 62,272 成员 668,968 内容