日期:2014-05-17  浏览次数:20390 次

错误:索引超出范围。必须为非负值并小于集合大小。参数名: index
C# code


private void bind()
        {
            DataSet ds = new DataSet();
            BLL.bunit bu = new BLL.bunit();
            ds = bu.GetAllList();
            if (ds != null)
            {
                GridView1.DataSource = ds;
                GridView1.DataKeyNames = new string[] { "ID" };//主键
                GridView1.DataBind();
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex > -1)
            {
                string name = GridView1.Rows[e.Row.RowIndex].Cells[1].Text.Trim();
                e.Row.Attributes.Add("onclick ", " window.opener.document.getElementById('txtpunit').value=" + name + ";window.opener=null;window.close();");
                bind();
            }
       }




------解决方案--------------------
Cells[1]存在吗,做个判断吧
------解决方案--------------------
e.Row.Cells 这个下标是从0开始的 看有没有问题
Rows[i].Cells.Count>1 可以断点看看这个是否为真
------解决方案--------------------
string name = GridView1.Rows[e.Row.RowIndex].Cells[1].Text.Trim();
调试下看你的Rows有多少行,Cell有多少个,
------解决方案--------------------
探讨

Cells[1]存在吗,做个判断吧

------解决方案--------------------
一般抱这种错,你就看看[]里的数是不是不对。就现在这个,cells[1]肯定不存在啊。