解决方案索引超出范围。必须为非负数,并且小于集合的大小(以 vb.net 为单位)

solution Index was out of range. Must be non-negative and less than the size of the collection in vb.net

提问人:roy 提问时间:4/19/2022 最后编辑:roy 更新时间:4/21/2022 访问量:2877

问:

我有一个错误问题“索引超出范围。必须为非负数且小于集合的大小“,当我输入一个值为数字”1“的值时,会出现错误,并且该值不在 datagridview 的”code“列中。有最好的建议吗?

Public x As Integer
Dim source1 As New BindingSource() 

Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles TextBox2.KeyDown
    If e.KeyCode = Keys.Back Then
        source1.Filter = ""
        TextBox1.Clear()
    Else
        If e.KeyCode = Keys.Enter Then
source1.Filter = "CODE = '" & Replace(TextBox2.Text, "'", "") & "'"
            x = DataGridView1.Rows.IndexOf(DataGridView1.CurrentRow)
            txtCODE.Text = DataGridView1.Rows(x).Cells(0).Value.ToString()
            Dim dte = DataGridView1.Rows(x).Cells(1).Value.ToString()

            If dte <> Nothing Then
                txtDTE.Text = CDate(dte)
            Else
                'CLEAR txtDTE if dt <> nothing
                txtDTE.Clear()
            End If

            txtQTY.Text = DataGridView1.Rows(x).Cells(2).Value.ToString()
            txtPRICE.Text = DataGridView1.Rows(x).Cells(3).Value.ToString()
            txtPRICE.Text = String.Format(System.Globalization.CultureInfo.GetCultureInfo("en-US"), "{0:N0}", Double.Parse(txtPRICE.Text))
 
            If source1.Count <= 0 Then
                source1.Filter = ""
                TextBox2.Clear()
                MsgBox("No Result Found!", MsgBoxStyle.Exclamation)
            End If
        End If
    End If
End Sub

Index was out of range & Must be non-negative and less than the size of the collection view datagridview

Index was out of range & Must be non-negative and less than the size of the collection-2

vb.net 事件 DataGridView 文本框

评论

0赞 JohnG 4/19/2022
你需要仔细看看代码行......如果 是 ,则将返回 -1。此外,这可以简化为......给定错误,这是唯一会在该行代码上抛出该错误的事情。如果网格至少有一行,则必须至少有一列。x = DataGridView1.Rows.IndexOf(DataGridView1.CurrentRow)DataGridView1.CurrentRowNothing/NullIndexOfx = DataGridView1.CurrentRow.Index
0赞 roy 4/19/2022
@JohnG,谢谢你的回复,你能给你一个答案吗?因为我还没有解决问题
0赞 JohnG 4/19/2022
这回答了你的问题吗?什么是 IndexOutOfRangeException / ArgumentOutOfRangeException,如何修复它?

答:

0赞 roy 4/19/2022 #1

我通过添加检查来修复它,以确保当前行的索引有效。

Public x As Integer
Dim source1 As New BindingSource() 

Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles TextBox2.KeyDown
    If e.KeyCode = Keys.Back Then
        source1.Filter = ""
        TextBox1.Clear()
    Else
        If e.KeyCode = Keys.Enter Then
source1.Filter = "CODE = '" & Replace(TextBox2.Text, "'", "") & "'"
            x = DataGridView1.Rows.IndexOf(DataGridView1.CurrentRow)
           ***If x >= 0 And x <= DataGridView1.Rows.Count - 1 Then*** 'ADD THIS CODE SOLUTION FOR ME
                txtCODE.Text = DataGridView1.Rows(x).Cells(0).Value.ToString()
                Dim dte = DataGridView1.Rows(x).Cells(1).Value.ToString()

                If dte <> Nothing Then
                    txtDTE.Text = CDate(dte)
                Else
                    'CLEAR txtDTE if dt <> nothing
                    txtDTE.Clear()
                End If

                txtQTY.Text = DataGridView1.Rows(x).Cells(2).Value.ToString()
                txtPRICE.Text = DataGridView1.Rows(x).Cells(3).Value.ToString()
                txtPRICE.Text = String.Format(System.Globalization.CultureInfo.GetCultureInfo("en-US"), "{0:N0}", Double.Parse(txtPRICE.Text))
            End If
        End If
    End If

    If source1.Count <= 0 Then
        source1.Filter = ""
        TextBox2.Clear()
        MsgBox("No Result Found!", MsgBoxStyle.Exclamation)
    End If
End Sub

评论

0赞 Craig 4/19/2022
你能为这个答案提供一些背景吗?发生了什么变化?它有什么帮助?
0赞 roy 4/20/2022
@Craig,我添加了代码,因此它不会出错。或者您有其他建议If x >= 0 And x <= DataGridView1.Rows.Count - 1 Then