索引超出范围。必须为非负数且小于集合的大小。参数名称: VB.Net 中的索引

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index in VB.Net

我有一个 GridView,它已经填充了一个 select 按钮来查看 selected 项目。

但是一旦调用该方法,我就会收到以下消息,我似乎可以弄明白

System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.'

这是假设 select Dim cusName As String = Report.SelectedRow.Cells(0).Text 在索引 0

这是我的示例 table

<div class="row">
    <div class="col-lg-12 col-md-12 col-xs-12">
        <cc1:TabContainer ID="TabContainer1" CssClass="Tab" runat="server" ActiveTabIndex="0" Width="100%">
            <cc1:TabPanel ID="tsaTabPanel" runat="server" HeaderText="Report" EnableViewState="False">
                <ContentTemplate>
                    <div>
                        <asp:GridView CssClass="table table-bordered table-striped" ID="RCReport" runat="server" AutoGenerateColumns="False" AllowPaging="True" OnPageIndexChanging="OnPageIndexChanging" PageSize="100" OnSelectedIndexChanged="OnSelectedIndexChanged">
                            <Columns>
                                <asp:BoundField DataField="id" HeaderText="Id" SortExpression="id"></asp:BoundField>
                                <asp:BoundField DataField="customerName" HeaderText="Customer Name" SortExpression="customerName" ></asp:BoundField>
                                <asp:BoundField DataField="customerEmail" HeaderText="Email" SortExpression="customerEmail"></asp:BoundField>
                                <asp:BoundField DataField="fee" HeaderText="Fee" SortExpression="fee"></asp:BoundField>
                                <asp:ButtonField Text="Select" CommandName="Select" ItemStyle-Width="150" />   
                            </Columns>
                        </asp:GridView>
                    </div>
                </ContentTemplate>
            </cc1:TabPanel>
        </cc1:TabContainer>
    </div>
</div>

代码隐藏

Protected Sub OnSelectedIndexChanged(sender As Object, e As EventArgs)
    Dim cusName As String = RCReport.SelectedRow.Cells(0).Text
    Dim id As String = RCReport.SelectedRow.Cells(1).Text
    ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Id: " & id & "\nCustomer Name: " & cusName + "');", True)
End Sub

为您编写了一个快速测试,以展示您应该能够实现所需目标的两种不同方式。

前端

<form id="form1" runat="server">    
    <asp:GridView ID="gvTest" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:BoundField DataField="City" HeaderText="City" />
            <asp:CommandField ShowSelectButton="true" SelectText="Select Me" SelectImageUrl="~/images/green.png" />
        </Columns>
    </asp:GridView>
</form>

代码隐藏

Imports System.Collections.Generic

Partial Class _testVBNet
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        ' only setting the value once on initial load.
        If Not Page.IsPostBack Then
            Dim myData As New List(Of SimpleObject)
            Dim obj As SimpleObject = New SimpleObject() With {.Name = "Bob", .City = "New York"}
            myData.Add(obj)
            Dim obj2 As SimpleObject = New SimpleObject() With {.Name = "Aaron", .City = "Denver"}
            myData.Add(obj2)
            Dim obj3 As SimpleObject = New SimpleObject() With {.Name = "Shawn", .City = "Seattle"}
            myData.Add(obj3)

            Me.gvTest.DataSource = myData
            Me.gvTest.DataBind()
        End If
    End Sub

    Protected Sub gvTest_SelectedIndexChanging(ByVal sender As Object, ByVal e As GridViewSelectEventArgs) Handles gvTest.SelectedIndexChanging
        Dim row As GridViewRow = Me.gvTest.Rows(e.NewSelectedIndex)
        Dim output As String = String.Format("{0} - {1}", row.Cells(0).Text, row.Cells(1).Text)  ' no .Net error
    End Sub

    Protected Sub gvTest_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles gvTest.SelectedIndexChanged
        Dim row As GridViewRow = Me.gvTest.SelectedRow
        Dim output As String = String.Format("{0} - {1}", row.Cells(0).Text, row.Cells(1).Text)  ' no .Net error
    End Sub

    Protected Class SimpleObject
        Sub New()
        End Sub
        Public Property Name As String
        Public Property City As String
    End Class

End Class

一些想法:

  • EnableViewState = false 可能是个问题,但不应该是
  • 通过按钮或其他事件动态绑定的数据可能需要重新绑定,但同样它应该在页面的生命周期中继续存在。

您没有正确创建索引更改事件。

你有这个:

Protected Sub OnSelectedIndexChanged(sender As Object, e As EventArgs)

如果您的网络表单上有 2 个网格 - 上面属于哪一个????

这样做: 在窗体视图设计器中——点击网格——显示属性sheet。然后点击闪电(对于事件),然后双击事件

就像 VB6,甚至 MS-Access 一样?您必须单击正确的事件存根。然后你得到这个:

 Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged

 End Sub

现在 SelectIndexChanged 事件如何使用 actaul 控件作为前缀。我的意思是,您可以在 Web 表单上有多个网格 - 您的代码将引用哪一个?

现在,在你的情况下,假设网格视图是“gvTest”,那么按照上面的方法双击索引更改事件应该给你这个:

 Protected Sub gvTest_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged

 End Sub

所以,现在在上面的事件中尝试你的代码 - 你的 cells() 集合是空的,因为代码存根是错误的,索引更改事件没有正确触发。

所以,在您双击后 - 如果您返回 属性 sheet,您会看到事件名称如下:

因此,无论哪种方式 - 您拥有的事件存根都是错误的,因此没有发生索引更改事件。