指数超出范围 [英] Index was out of range

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

问题描述

我的代码如下



My code as follows

private void SaveCheckedValues()
        {
            System.Collections.ArrayList userdetails = new System.Collections.ArrayList();
            int index = -1;
            foreach (GridViewRow gvrow in grdRpt.Rows)
            {
                object o = grdRpt.DataKeys[gvrow.RowIndex].Value;
                index = (int)0;
                bool result = ((CheckBox)gvrow.FindControl("chkselecdata")).Checked;

                // Check in the Session
                if (Session["CHECKED_ITEMS"] != null)
                    userdetails = (System.Collections.ArrayList)Session["CHECKED_ITEMS"];
                if (result)
                {
                    if (!userdetails.Contains(index))
                        userdetails.Add(index);
                }
                else
                    userdetails.Remove(index);
            }
            if (userdetails != null && userdetails.Count > 0)
                Session["CHECKED_ITEMS"] = userdetails;
        }





当我运行上面的代码时显示错误如下



指数超出范围。

必须是非负数且小于集合参数名称'Index'的大小



错误显示在下面的行如下



object o = grdRpt.DataKeys [gvrow.RowIndex] .Value;





在gridview中的值如下





when i run the above code shows error as follows

index was out of range.
Must be non-negative and less than the size of the collection parameter name 'Index'

The error show in below line as follows

object o = grdRpt.DataKeys[gvrow.RowIndex].Value;


In the gridview values as follows

transacteeid      totalprice   Qty   Isactive

109453628727        25           1      1
109453628727        15           2      1
565676778            8           3      1





我调试并检查值,我得到的值为109453628727



如何解决这个错误。我的上述代码中的错误是什么。



我尝试了什么:



我的代码如下



private void SaveCheckedValues()

{

System.Collections.ArrayList userdetails = new System.Collections.ArrayList();

int index = -1;

foreach(grdRpt.Rows中的GridViewRow gvrow)

{

object o = grdRpt.DataKeys [gvrow.RowIndex] .Value;

index =(int)0;

bool result =( (CheckBox)gvrow.FindControl(chkselecdata))。已检查;



//在会话中签入

if(Session [ CHECKED_ITEMS]!= null)

userdetails =(System.Collections.ArrayList)Session [CHECKED_ITEMS];

if(result)

{

if(!userdetails。包含(索引))

userdetails.Add(index);

}

else

userdetails.Remove(索引);

}

if(userdetails!= null&& userdetails.Count> 0)

Session [CHECKED_ITEMS] = userdetails;

}



当我运行上面的代码显示错误如下



指数超出范围。

必须是非负数且小于集合参数名称的大小'索引'



错误显示在下面的行如下



object o = grdRpt.DataKeys [gvrow .RowIndex] .Value;





在gridview中的值如下



transacteeid totalprice数量有效



109453628727 25 1 1

109453628727 15 2 1

565676778 8 3 1



当我调试并检入值时,我得到的值为109453628727



如何解决这个错误。上面代码中的错误是什么。



When i debug and check in the value i get value as "109453628727"

how to solve this error. what is the mistake in my above code.

What I have tried:

My code as follows

private void SaveCheckedValues()
{
System.Collections.ArrayList userdetails = new System.Collections.ArrayList();
int index = -1;
foreach (GridViewRow gvrow in grdRpt.Rows)
{
object o = grdRpt.DataKeys[gvrow.RowIndex].Value;
index = (int)0;
bool result = ((CheckBox)gvrow.FindControl("chkselecdata")).Checked;

// Check in the Session
if (Session["CHECKED_ITEMS"] != null)
userdetails = (System.Collections.ArrayList)Session["CHECKED_ITEMS"];
if (result)
{
if (!userdetails.Contains(index))
userdetails.Add(index);
}
else
userdetails.Remove(index);
}
if (userdetails != null && userdetails.Count > 0)
Session["CHECKED_ITEMS"] = userdetails;
}

when i run the above code shows error as follows

index was out of range.
Must be non-negative and less than the size of the collection parameter name 'Index'

The error show in below line as follows

object o = grdRpt.DataKeys[gvrow.RowIndex].Value;


In the gridview values as follows

transacteeid totalprice Qty Isactive

109453628727 25 1 1
109453628727 15 2 1
565676778 8 3 1

When i debug and check in the value i get value as "109453628727"

how to solve this error. what is the mistake in my above code.

推荐答案

您正在尝试访问不在数组中的索引。如果您尝试使用 3 元素访问数组中的 5th 成员,则会发生这种情况。



尝试检查 gvrow.RowIndex 的值,如果数组 grdRpt.DataKeys 有那么多元素。
you are trying to access an index which is not in the array. This will happen normally if you try to access 5th member in an array with 3 elements.

try to check value of gvrow.RowIndex and if the array grdRpt.DataKeys have that much element.


这篇关于指数超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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