当前位置:编程学习 > C#/ASP.NET >>

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

  protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int r = e.RowIndex;
            string sqlstr = "update TestID set 节次='"
             + ((TextBox)(GridView1.Rows[r].Cells[4].Controls[0])).Text.ToString().Trim() + "',课程='"
             + ((TextBox)(GridView1.Rows[r].Cells[5].Controls[0])).Text.ToString().Trim() + "',考勤情况='"
             + ((TextBox)(GridView1.Rows[r].Cells[6].Controls[0])).Text.ToString().Trim() + "' where 教工号='"
             + GridView1.DataKeys[r].Value.ToString() + "'";
            
        }
以下是对应图片:
--------------------编程问答-------------------- 来个大神指教一下,,,我是新手,,谢谢了 --------------------编程问答-------------------- 错误信息都给你了,跟踪调试一下就知道错误在哪了 --------------------编程问答--------------------
引用 2 楼 qq137051908 的回复:
错误信息都给你了,跟踪调试一下就知道错误在哪了
错误源就是这一句代码。可是我找不到到底哪里错了,求指教 --------------------编程问答-------------------- 跟踪看看GridView1.Rows[r].Cells[4].Controls[0]是什么
  
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/ --------------------编程问答--------------------
引用 4 楼 feiyun0112 的回复:
跟踪看看GridView1.Rows[r].Cells[4].Controls[0]是什么
  
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
指定到图片显示的节次段更改更新,可是就是超出范围 --------------------编程问答--------------------   string Emp_ID = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();

你先拆开来一个个看,你的cell要么数错了  要么是你的datakey有问题 --------------------编程问答--------------------
引用 3 楼 u013182245 的回复:
Quote: 引用 2 楼 qq137051908 的回复:

错误信息都给你了,跟踪调试一下就知道错误在哪了
错误源就是这一句代码。可是我找不到到底哪里错了,求指教

贴出你GridView1数据绑定模板的代码 --------------------编程问答-------------------- 给的信息量太少了啊,你调试看看你的RowIndex是多少,另外 有了Text属性就不用再.ToString()了啊 --------------------编程问答-------------------- 数组越界。
你的Cell可能长度没有5 --------------------编程问答--------------------
引用 9 楼 yuwenge 的回复:
数组越界。
你的Cell可能长度没有5

int r = e.RowIndex;
            string str_1 = GridView1.Rows[r].Cells[0].Text;
            string str_2 = GridView1.Rows[r].Cells[1].Text;
            string str_3 = GridView1.Rows[r].Cells[2].Text;
            string str_4 = GridView1.Rows[r].Cells[3].Text;
            string str_5 = GridView1.Rows[r].Cells[4].Text;
            string str_6 = GridView1.Rows[r].Cells[5].Text;
            string str_7 = GridView1.Rows[r].Cells[6].Text;




            string str_update = "update TestID set 姓名='" + str_2 + "', 周次='" + str_3 + "'星期='" + str_4 + "',节次='" + str_5 + "',课程='" + str_6 + "',考勤情况='" + str_7 + "', where 教工号=" + str_1 + "";改成这样,没有报错但是没有更新 --------------------编程问答--------------------
引用 8 楼 l9920704 的回复:
给的信息量太少了啊,你调试看看你的RowIndex是多少,另外 有了Text属性就不用再.ToString()了啊
   string str_1 = GridView1.Rows[r].Cells[0].Text;
            string str_2 = GridView1.Rows[r].Cells[1].Text;
            string str_3 = GridView1.Rows[r].Cells[2].Text;
            string str_4 = GridView1.Rows[r].Cells[3].Text;
            string str_5 = GridView1.Rows[r].Cells[4].Text;
            string str_6 = GridView1.Rows[r].Cells[5].Text;
            string str_7 = GridView1.Rows[r].Cells[6].Text;




            string str_update = "update TestID set 姓名='" + str_2 + "', 周次='" + str_3 + "'星期='" + str_4 + "',节次='" + str_5 + "',课程='" + str_6 + "',考勤情况='" + str_7 + "', where 教工号=" + str_1 + "";改成这样,没有报错但是没有更新 --------------------编程问答--------------------
引用 6 楼 z_dota 的回复:
  string Emp_ID = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();

你先拆开来一个个看,你的cell要么数错了  要么是你的datakey有问题
   string str_1 = GridView1.Rows[r].Cells[0].Text;
            string str_2 = GridView1.Rows[r].Cells[1].Text;
            string str_3 = GridView1.Rows[r].Cells[2].Text;
            string str_4 = GridView1.Rows[r].Cells[3].Text;
            string str_5 = GridView1.Rows[r].Cells[4].Text;
            string str_6 = GridView1.Rows[r].Cells[5].Text;
            string str_7 = GridView1.Rows[r].Cells[6].Text;




            string str_update = "update TestID set 姓名='" + str_2 + "', 周次='" + str_3 + "'星期='" + str_4 + "',节次='" + str_5 + "',课程='" + str_6 + "',考勤情况='" + str_7 + "', where 教工号=" + str_1 + "";改成这样,没有报错但是没有更新 --------------------编程问答--------------------
引用 12 楼 u013182245 的回复:
Quote: 引用 6 楼 z_dota 的回复:

  string Emp_ID = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();

你先拆开来一个个看,你的cell要么数错了  要么是你的datakey有问题
   string str_1 = GridView1.Rows[r].Cells[0].Text;
            string str_2 = GridView1.Rows[r].Cells[1].Text;
            string str_3 = GridView1.Rows[r].Cells[2].Text;
            string str_4 = GridView1.Rows[r].Cells[3].Text;
            string str_5 = GridView1.Rows[r].Cells[4].Text;
            string str_6 = GridView1.Rows[r].Cells[5].Text;
            string str_7 = GridView1.Rows[r].Cells[6].Text;




            string str_update = "update TestID set 姓名='" + str_2 + "', 周次='" + str_3 + "'星期='" + str_4 + "',节次='" + str_5 + "',课程='" + str_6 + "',考勤情况='" + str_7 + "', where 教工号=" + str_1 + "";改成这样,没有报错但是没有更新

str_update肯定断掉了,因为中间有部分str_X是null。 --------------------编程问答--------------------
引用 11 楼 u013182245 的回复:
Quote: 引用 8 楼 l9920704 的回复:

给的信息量太少了啊,你调试看看你的RowIndex是多少,另外 有了Text属性就不用再.ToString()了啊
   string str_1 = GridView1.Rows[r].Cells[0].Text;
            string str_2 = GridView1.Rows[r].Cells[1].Text;
            string str_3 = GridView1.Rows[r].Cells[2].Text;
            string str_4 = GridView1.Rows[r].Cells[3].Text;
            string str_5 = GridView1.Rows[r].Cells[4].Text;
            string str_6 = GridView1.Rows[r].Cells[5].Text;
            string str_7 = GridView1.Rows[r].Cells[6].Text;




            string str_update = "update TestID set 姓名='" + str_2 + "', 周次='" + str_3 + "'星期='" + str_4 + "',节次='" + str_5 + "',课程='" + str_6 + "',考勤情况='" + str_7 + "', where 教工号=" + str_1 + "";改成这样,没有报错但是没有更新

你调试的时候把你的str_update复制出来 然后放到SQL SERVER里面直接执行试试 还有 你where前面的","是干嘛的啊 --------------------编程问答-------------------- 从这些信息里面,我只能是无能为力,但是你可以调试的时候看下这两个值:GridView1.Rows.Count和GridView1.Rows[0].Cells.Count --------------------编程问答-------------------- 你要干嘛??修改信息?
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,