0

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

roy
  • 693
  • 2
  • 11
  • You need to take a closer look at the line of code… `x = DataGridView1.Rows.IndexOf(DataGridView1.CurrentRow)` … if `DataGridView1.CurrentRow` is `Nothing/Null`, then `IndexOf` will return a -1. In addition this could be simplified to `x = DataGridView1.CurrentRow.Index` … Given the error, that is about the only thing that would throw that error on that line of code. If the grid has at least one row, then there must be at least one column. – JohnG Apr 19 '22 at 07:23
  • @JohnG ,Thank you in reply from you, can you give as an answer from you? because I still haven't solved the problem – roy Apr 19 '22 at 07:36
  • Does this answer your question? [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – JohnG Apr 19 '22 at 07:53

1 Answers1

0

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
Craig
  • 2,248
  • 1
  • 19
  • 23
roy
  • 693
  • 2
  • 11
  • Can you provide some context to this answer? What changed? How did it help? – Craig Apr 19 '22 at 14:49
  • @Craig , `If x >= 0 And x <= DataGridView1.Rows.Count - 1 Then` I added the code so it doesn't error. or you have another recommendation – roy Apr 20 '22 at 08:18