读书人

异常:索引超出范围。必须为非负值并小

发布时间: 2012-08-21 13:00:21 作者: rapoo

错误:索引超出范围。必须为非负值并小于集合大小。参数名: 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]肯定不存在啊。

读书人网 >asp.net

热点推荐