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

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

问题描述

在gridviewID''gvtest''中我收到一条错误,说明:

''索引超出范围。必须是非负的且小于集合的大小''

我提交我使用的代码。

In a gridviewID ''gvtest'' I get an error stating as :
''Index is out of range. Must be non-negative and less than the size of collection''
I submit the codes I have used.

<asp:GridView ID="gvtest1" runat="server" AutoGenerateColumns="False"

            style="z-index: 1; left: 68px; top: 60px; position: absolute; height: 105px; width: 390px; background-color: #ECE9D8;"

           DataSourceID="SqlDataSource1"

            HorizontalAlign="Center" BorderColor="Maroon" ForeColor="#CC9900">
         <Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>







protected void btnProcess_Click(object sender, EventArgs e)
  {

      string str = string.Empty;
      string strname = string.Empty;
      string v_amt = string.Empty;

      foreach (GridViewRow gvrow in gvtest1.Rows)
      {
          CheckBox chk = (CheckBox)gvrow.FindControl("chkSelect");
          if (chk != null & chk.Checked)
          {
              //str += gvtest.Rows[gvrow.RowIndex].Value.ToString() + ',';
              str += gvtest1.DataKeys[gvrow.RowIndex].Value.ToString() + ',';
              strname += gvrow.Cells[2].Text + ',';
              v_amt += gvrow.Cells[5].Text + ',';
              string v_stringamt = gvrow.Cells[5].Text;
              int v_amt1 = Convert.ToInt32(v_stringamt);

              v_totamt = (v_totamt + v_amt1);
          }
      }





任何人都可以帮帮我吗?



Can anyone help me out?

推荐答案

它只是意味着您正在尝试访问不存在的索引。



例如:

您定义一个包含10个元素的数组A.当您尝试访问A [15]时,您将获得索引超出范围错误。由于您定义了10个元素,因此有效值仅为0到9。



现在,使用Visual Studio调试并查看获取它的时间和位置。在那里纠正你的逻辑。
It simply means that you are trying to access an index that does not exists.

For example:
You define an array A of 10 elements. When you try to access A[15] you will get an index out of range error. Since you defined 10 elements, valid values are from 0 to 9 only.

Now, use Visual Studio debug and see when & where you get it. Correct your logic there.


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

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