解决方案索引超出范围。必须是非负的,并且小于vb.net中收藏的大小

发布于 2025-01-22 04:31:04 字数 1806 浏览 4 评论 0 原文

我有一个错误问题“索引超出了范围。 DataGridView中的“代码”列。有最好的建议吗?

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

I have an error problem "Index was out of range. Must be non-negative and less than the size of the collection" when I input a value with the value of the number "1" then an error appears and the value is not in the "code" column in the datagridview. Is there a best recommendation?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮生面具三千个 2025-01-29 04:31:04

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

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

I fixed it by adding a check to make sure that the index of the current row is valid.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文