如何解决此问题,请帮助我"异常详细信息:System.ArgumentOutOfRangeException:指定的参数不在有效值范围内.参数名称:索引“ [英] How to solve this Please help me"Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index"
问题描述
我想基于复选框选择来计算我的gridview第二行列总数....
我的代码是
i want to calculate my gridview 2nd row column total based on check box selection....
My code is
decimal sum = 0;
protected void Button1_Click(object sender, EventArgs e)
{
foreach(GridViewRow Row in GridView1.Rows)
{
CheckBox cb1 =(CheckBox)Row.FindControl("Check");
if(cb1!=null && cb1.Checked)
{
decimal var = Convert.ToDecimal(Row.Cells[2].ToString());
sum += var;
Label1.Text =sum.ToString();
}
}
}
异常详细信息:System.ArgumentOutOfRangeException:指定的参数超出有效值范围.参数名称:index"
Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index"
推荐答案
decimal var = Convert.ToDecimal(Row.Cells[2].ToString());
确保您的表中至少有三列:当前似乎有两列或更少. (请记住,C#数组是从零开始的,所以具有两个元素的数组将是myArray [0]和myArray [1].尝试使用myArray [2]会给您索引错误,如您所见)
Make sure there are at least three columns in your table: at present it seems there are two or less. (Remember that C# arrays are zero based, so an array with two elements would be myArray[0] and myArray[1]. Trying to use myArray[2] will give you an index error as you have seen)
看起来像Row.Cells[2]
不存在.
确保使用适当的单元格索引值.
Looks likeRow.Cells[2]
does not exist.
Make sure you use the appropriate cell index value.
在代码中检查此行
Row.Cells[2]
根据错误,我猜测您的网格只有2列.
如果是这种情况,请使用
Row.cells[1]
check this line in your code
Row.Cells[2]
according to the error i am guessing that your grid has only 2 columns.
if that is the case then use
Row.cells[1]
这篇关于如何解决此问题,请帮助我"异常详细信息:System.ArgumentOutOfRangeException:指定的参数不在有效值范围内.参数名称:索引“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!