string c_id=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
为什么这句话有时候运行正确 有时候报错
“索引超出范围。必须为非负值并小于集合大小。参数名: index”
ASP.NET 1.1 C# 
这个错误应该是e.Item.ItemIndex这个参数有问题
于是我Response.Write(e.Item.ItemIndex);
结果输出 1  没有问题啊
更奇怪的是我又执行一句
this.DataGrid1.EditItemIndex=e.Item.ItemIndex;
然后在执行
string c_id=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
然后一切正常 没有报错
谁知道这是怎么回事?
我快郁闷死了~!

解决方案 »

  1.   

    e.Item.ItemIndex 有可能为-1,如果当前的item是header的话.
      

  2.   

    可是我Response.Write(e.Item.ItemIndex);
    结果是 1 啊   没有错
      

  3.   

    结果是 1 啊   没有错
    =====
    那有没有指定DataKeyField?
    datagrid的记录有没有2条以上?
      

  4.   

    我又把.net版本换成2.0
    依然是老问题
      

  5.   

    为什么我执行
    this.DataGrid1.EditItemIndex=e.Item.ItemIndex;
    然后再执行
    string c_id=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
    就可以了呢
      

  6.   

    运行的上下文??
    取值时datagrid 绑定数据了没?
      

  7.   

    DataGrid只有一条记录时
    Response.Write(e.Item.ItemIndex);
    结果是0  没有错
      

  8.   

    建议DataKeys不要用e.Item.ItemIndex,可以用database中的id.
    我也曾经出现过类似的错误。后来用id解决了。
      

  9.   

    DataGrid 绑定了
    没绑定怎么会显示呢?
    DataKeyFiled也绑定了
    SqlConnection con=DataCon.CreateCon();
    SqlCommand cmd=new SqlCommand("select at_id,at_name from attachments where a_id="+a_id +"order by at_id",con);
    con.Open();
    this.DataGrid1.DataSource=cmd.ExecuteReader();
    this.DataGrid1.DataBind();
    con.Close();
    this.DataGrid1.DataKeyField="at_id";
      

  10.   

    this.DataGrid1.DataKeyField="at_id";
    应该放在绑定之前.
      

  11.   

    this.DataGrid1.DataKeyField="at_id";this.DataGrid1.DataBind();
      

  12.   

    this.DataGrid1.DataKeyField="at_id";
    这句我试过放在绑定之前也不行
      

  13.   

    nyzhl() ( ) 
    =================
    是在更新数据用的???
    没遇到过
      

  14.   

    判断它的列是正常项或者是交替项。。然后再进行数据读取。。可能是HEAD也说不定。。所以最好加判断。
      

  15.   

    加入
    if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
    错误依旧我这里网络不是很稳定 所以这会才回复 大家多见谅
      

  16.   

    建议DataKeys不要用e.Item.ItemIndex,可以用database中的id.
    我也曾经出现过类似的错误。后来用id解决了。
    =====================================================
    楼上能不能说具体一点
    DataKeys不用e.Item.ItemIndex
    怎么取到database中的id呢
      

  17.   

    e.Item.ItemIndex结果是0
    DataGrid1.DataKeys[0]结果报错超出范围
    难道数据没绑定上?
    但我点击DataGrid上的删除按钮才执行的
    如果没绑定上怎么会显示DataGrid的数据呢 private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {

    string at_id=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
    SqlConnection con=DataCon.CreateCon();
    SqlCommand cmd=new SqlCommand("select at_url from attachments where at_id="+at_id,con);
    object temp=new object();
    con.Open();
    temp=cmd.ExecuteScalar();
    con.Close();
    string at_url=Server.MapPath("../upload/att/")+temp.ToString();
    if(File.Exists(at_url))
    {
    File.Delete(at_url);
    }
    cmd.CommandText="delete from attachments where at_id="+at_id;
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    Response.Write("<script>window.alert('删除成功!');</script>");
    this.bind();

    }
      

  18.   

    DataKeyFiled要在gridview(2.0)属性窗口中说明下
      不是这样你找我  
    我以前也遇到这个现象
      

  19.   

    DataKeyFiled要在gridview(2.0)属性窗口中说明下
      不是这样你找我  
    我以前也遇到这个现象
    ====================================================
    谢谢楼上的
    我在DataGrid属性窗口设置设置DataKeyFiled问题解决
    看来可能是。net的bug
      

  20.   

    谢谢楼上各位兄弟的热心帮助
    但是分只能给楼上的onlyonereason(学习sql中)
    呵呵~
      

  21.   

     GridView1.DataKeyNames = new string[] { "ID" }; 加这个即可
      

  22.   

     GridView1.DataKeyNames = new string[] { "ID" }; 加这个即可
      

  23.   

    那是BUG吗  本人也遇到那个问题  我的vs里没DataKeyFiled项啊
      

  24.   

    这个问题烦左我整天,最后突然解决了这个问题!在一个(void)断代码内,不能同时出现两个取视图主键列的代码: gvBorrowBook.DataKeys[e.RowIndex ].Value.ToString(); 这回发生索引超出的冲突
      

  25.   

    出现上面的问题,请教各位。
    public partial class kcdetails : System.Web.UI.Page
    {
        String sqlstr;
        DataSet ds = new DataSet();
        DB db = new DB();
        String kc_id;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                kc_id = Request.QueryString.Get(0).ToString().Trim();
                sqlstr = "select * from 课程信息表 where 课程号=" + kc_id;
                ds = db.GetDataTableBySql(sqlstr);
                try
                {
                    if (ds.Tables[0].Rows.Count != 0)
                    {
                        this.Repeater1.DataSource = ds.Tables[0].DefaultView;
                        this.Repeater1.DataBind();
                        Session["kc_id"] = kc_id;                }            }
                catch (Exception)
                {
                    Response.Write("<script>alert('没有获得任何数据,请检查!')</script>");
                }
            }    }