gradview中index索引超出范围问题

问题描述

后台代码:namespaceWebApplication1{publicpartialclassAltertable:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){if(!IsPostBack){GridView1.DataSource=GetData();GridView1.DataBind();}}publicDataViewGetData(){SqlConnectioncon=newSqlConnection(System.Configuration.ConfigurationManager.AppSettings["DBConnection"]);con.Open();SqlDataAdapterda=newSqlDataAdapter("select*fromdbo.AD_Contenta,dbo.ADdayClickCountbwherea.ADGuid=b.ADGuid",con);DataSetds=newDataSet();da.Fill(ds);da.Dispose();con.Close();con.Dispose();returnds.Tables[0].DefaultView;}publicstringTypeConvert(stringtypeID){if(typeID=="1")return"浮动广告";elseif(typeID=="2")return"弹出广告";elsereturn"";}protectedvoidButton1_Click(objectsender,EventArgse){Response.Redirect("insertAD.aspx");}protectedvoidGridView1_RowCommand(objectsender,GridViewCommandEventArgse){if(e.CommandName=="Select"){stringstr="Youwantedtochangetheline"+e.CommandArgument.ToString()+"!";Response.Write("<script>window.alert('"+str+"')</script>");intindex=Convert.ToInt32(e.CommandArgument);stringstr2=GridView1.DataKeys[index].Value.ToString();/*ADGuid*//*Session["AdId"]=str2;*/Response.Redirect("Altertable.aspx?ID="+str2);}/*elseif(e.CommandName=="Delete1"){intindex=Convert.ToInt32(e.CommandArgument);GridViewRowrow=GridView1.Rows[index];ListItemitem=newListItem();item.Text=Server.HtmlDecode(row.Cells[2].Text)+""+Server.HtmlDecode(row.Cells[3].Text);stringstr1=row.Cells[1].Text.ToString();stringstr=GridView1.DataKeys[index].ToString();stringstr2="Youwantedtochangetheline"+e.CommandArgument.ToString()+"!";Response.Write("<script>window.alert('"+str1+"')</script>");}*/}protectedvoidGridView1_RowDeleting(objectsender,GridViewDeleteEventArgse){/*intstrid=(int)GridView1.DataKeys[e.RowIndex].Value;*/intindex=e.RowIndex;GridViewRowrow=GridView1.Rows[index];stringstr1=row.Cells[1].Text.ToString();str1=row.Cells[1].Text.ToString();stringstr2=GridView1.DataKeys[index].Value.ToString();/*取出的广告ID*//*stringstr="Youwantedtodeletetheline"+"str1;"+str1+"str2;"+str2;Response.Write("<script>window.alert('"+str+"')</script>");*测试数据用*//*根据ADGuid删除数据库里的内容*/stringconstr=System.Configuration.ConfigurationManager.AppSettings["DBConnection"];SqlConnectioncon=newSqlConnection(constr);con.Open();stringsqlStr1="declare@myidvarchar(50)set@myid='"+str2+"'"+";deletefromAD_CountwhereADGuid=@myid;"+"deletefromAD_ContentwhereADGuid=@myid;";stringsqlStr2="deletefromAD_CountwhereADGuid=@myid1;"+"deletefromAD_ContentwhereADGuid=@myid2;";SqlCommandsqlCmd1=newSqlCommand(sqlStr1,con);/*SqlParameter[]arr=newSqlParameter[]{newSqlParameter("@myid1",str2),newSqlParameter("@myid2",str2)};*/sqlCmd1.ExecuteNonQuery();DataSetds=newDataSet();sqlCmd1.Dispose();con.Close();con.Dispose();Response.Write("<script>window.alert('删除成功!')</script>");Response.Redirect("Default.aspx");}protectedvoidButton2_Click(objectsender,EventArgse){}protectedvoidButton3_Click(objectsender,EventArgse){}protectedvoidGridView1_SelectedIndexChanged(objectsender,EventArgse){}protectedvoidlinkBtnUpdate_Click(objectsender,EventArgse){}}}然后调试时一直出现该怎么解决呀。。

解决方案

解决方案二:
参考:问题出现在:没有设置DATAKEYFIELD设为数据库中的ID值。因为在引用this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();之前要为DATAKEYS填充数据源,既数据库中的ID字段。解决方法:不管是DataGrid还是GridView在控件的属性窗口(其实就是aspx文件中控件属性)中直接给DataKeyField(DataKeyName)赋值为数据库的ID值,问题即可解决。
解决方案三:
DataKey是为空吧,从哪里抄的代码
解决方案四:
引用2楼duanzi_peng的回复:

DataKey是为空吧,从哪里抄的代码

是别人的代码,我拿过来改了下发现有问题,但别人同样代码却没问题
解决方案五:
引用1楼stubble的回复:

参考:问题出现在:没有设置DATAKEYFIELD设为数据库中的ID值。因为在引用this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();之前要为DATAKEYS填充数据源,既数据库中的ID字段。解决方法:不管是DataGrid还是GridView在控件的属性窗口(其实就是aspx文件中控件属性)中直接给DataKeyField(DataKeyName)赋值为数据库的ID值,问题即可解决。

那我该怎么改呢。。。我数据库中的id是ADGuid,新手求教。。

时间: 2024-07-30 05:29:36

gradview中index索引超出范围问题的相关文章

删除-急!!!!index;索引超出范围。必须为非负

问题描述 急!!!!index;索引超出范围.必须为非负 protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "shanchu") //如果单击的是"选修"按钮 { int index = Convert.ToInt32(e.CommandArgument.ToString()); //取出选修课程所在的行索引 Da

gridview中gridview索引超出范围。必须为非负值并小于集合大小。 参数名: index,求帮助

问题描述 protectedvoidPage_Load(objectsender,EventArgse){stringstr="select*fromdbo.Medicine";dbconectiondb=newdbconection();DataSetds=db.DataAdapter(str);GridView2.DataSource=ds;GridView2.DataBind();GridView2.DataKeyNames=newstring[]{"mno"

“/”应用程序中的服务器错误。索引超出了数组界限。

问题描述 "/"应用程序中的服务器错误.索引超出了数组界限. "/"应用程序中的服务器错误. 索引超出了数组界限. 说明: 执行当前 Web 请求期间,出现未处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息. 异常详细信息: System.IndexOutOfRangeException: 索引超出了数组界限. 源错误: [没有相关的源行] 源文件: c:WINDOWSMicrosoft.NETFrameworkv2.0.50727

求助:TeeChart中的Bar出现的索引超出了数组界限的问题

问题描述 最近在使用TeeChart绘图,但刚做了一个例子就出问题了.我是使用TeeChartv3.2在VS2005中,添加一个TeeChart,在其Series添加一个Bar,在Bar中随机添加100个数据,代码如下:privatevoidForm1_Load(objectsender,EventArgse){Barbar1=newBar();//usingSteema.TeeChart.Styles,TheBarSeriescomponentoutputsallpointsasvertica

索引超出范围。必须为非负值并小于集合大小

问题描述 说明:执行当前Web请求期间,出现未处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息.异常详细信息:System.ArgumentOutOfRangeException:索引超出范围.必须为非负值并小于集合大小.参数名:index源错误:行81://取主键行82:行83:inttypeId=Convert.ToInt32(this.gvRoomType.DataKeys[e.RowIndex].Value);行84:boolres=BLL.RoomTy

System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大

问题描述 索引超出范围.必须为非负值并小于集合大小.参数名:index说明:执行当前Web请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息.异常详细信息:System.ArgumentOutOfRangeException:索引超出范围.必须为非负值并小于集合大小.参数名:index源错误:行192:SqlConnectionconn=newSqlConnection(connectString);行193:conn.Open();行194:s

C# 关闭Form时出错:索引超出范围。必须为非负值并小于集合大小

问题描述 System.ArgumentOutOfRangeException:索引超出范围.必须为非负值并小于集合大小.参数名:index在System.Collections.ArrayList.get_Item(Int32index)在System.Windows.Forms.DataGridViewColumnCollection.get_Item(Int32index)在System.Windows.Forms.DataGridView.PositionEditingControl(B

gridview 索引超出范围。必须为非负值并小于集合大小。!!!

问题描述 索引超出范围.必须为非负值并小于集合大小.参数名:index说明:执行当前Web请求期间,出现未处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息.异常详细信息:System.ArgumentOutOfRangeException:索引超出范围.必须为非负值并小于集合大小.参数名:index拜求高手解决!!!程序如下!protectedvoidSearch_Click(objectsender,EventArgse){SqlConnectionmyCon

Oracle中建立索引并强制优化器使用

当WHERE子句对某一列使用函数时,除非利用这个简单的技术强制索引,否则Oracle优化器不能在查询中使用索引. 通常情况下,如果在WHERE子句中不使用诸如UPPER.REPLACE 或SUBSTRD等函数,就不能对指定列建立特定的条件.但如果使用了这些函数,则会出现一个问题:这些函数会阻碍Oracle优化器对列使用索引,因而与采用索引的情况相比较,查询会花费更多的时间. 庆幸的是,如果在使用函数的这些列中包含了字符型数据,可以用这样一种方法修改查询语句,以达到强制性使用索引,更有效地运行查询