Gridview RowIndex错误-索引超出范围... [英] Gridview RowIndex Error - Index was out of range...

查看:136
本文介绍了Gridview RowIndex错误-索引超出范围...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个页面的GridView,我想检索第二页的行索引.
我尝试的以下代码遇到此错误.
Index was out of range.

这是我的代码:

 如果(例如,CommandName == " )
{
     int  i = Convert.ToInt32(例如CommandArgument);
    GridViewRow行= GridViewArtenVerp.Rows [i];
    // 一些用于检索单元格值的代码
} 



有人可以帮我吗?
在此先谢谢您.
谢谢
faisal


在代码上放置一个调试点,然后选中i.似乎是一个不存在任何行的值.


您的命令参数是数据源中的索引,而不是gridview.如果您在gridview中的页面大小为10,则该控件的有效索引范围将在0到9之间.
如果尝试选择命令参数为11的项目,则需要获取索引的模数,然后将其转换为正确的gridview索引.即GridViewRow row = GridViewArtenVerp.Rows[i % 10];(注意.我会为此添加一些验证.我从未允许像这里显示的那样在索引字段中进行计算.)


I have GridView with several pages and I want to retrieve the row index of the second page.
I am getting this error for the below code that I tried.
Index was out of range.

here my code is:

if (e.CommandName == "Go")
{
    int i = Convert.ToInt32 (e.CommandArgument);
    GridViewRow row = GridViewArtenVerp.Rows[i];
    // some code to retrieve cellvalues
}



Can someone help me?
Thanks in advance.

解决方案

how or what you set on e.CommandArgument are u setting the rowindex values in e.CommandArgument this migth be the case.

thanks
faisal


Put a debug point on your code and check i. It seems to be a value for which no row exists.


Your command argument is the index in your datasource, not your gridview. If your page size in the gridview is 10 then the valid range of indices for the control will be between 0 and 9.
If you are trying to select the item with a command argument of 11, then you need to take the modulus of the the index and that will translate it to the correct gridview index. i.e. GridViewRow row = GridViewArtenVerp.Rows[i % 10]; (Note. I would add some validation around this. I never allow for calculations in an index field like I have shown here.)


这篇关于Gridview RowIndex错误-索引超出范围...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆