0

Hello.

Could somebody explain why this code snippet is not working?
The arrow (<----) points to where it's going wrong. It goes wrong when building, with the error of:
"An error occurred creating the form. See Exception.InnerException for details. The error is: Index was out of range. Must be non-negative and less than the size of the collection."

 Private Sub DataTable_CellChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataTable.CellValueChanged
        If (e.ColumnIndex = 1) Then
            If (IsNumeric(DataTable.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) <----) Then
                DataTable.Item(e.ColumnIndex, e.RowIndex).Value = 5
                DataTable.Item(e.ColumnIndex, e.RowIndex).Style.BackColor = Color.Red
            End If
        End If
    End Sub
3
Contributors
16
Replies
60
Views
3 Years
Discussion Span
Last Post by ricky.subiantoputra
Featured Replies
  • got it you can try add on error resume next or a little if then else like this 1 Private Sub DataTable_CellChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataTable.CellValueChanged 2 If (e.ColumnIndex = 1) and e.RowIndex>=0 Then 3 If (IsNumeric(DataTable.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)) Then 4 DataTable.Item(e.ColumnIndex, e.RowIndex).Value = 5 5 DataTable.Item(e.ColumnIndex, e.RowIndex).Style.BackColor = … Read More

0

DataGridView. I named it a DataTable because I like to make names shorter.

0

I was somewhat confused, because DataTable is also an existing class in .NET. I always name my DataGridView DGV. It is short and obvious, for me that is :).
Your problem is probably that IsNumeric does not like a Value type.
Try converting to a string: IsNumeric(DataTable.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString())

0

Thanks for responding, but it still doesent work. :(
Just incase, heres my code:

    Private Sub DataTable_CellChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataTable.CellValueChanged
        If (e.ColumnIndex = 1) Then
            If (IsNumeric(DataTable.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString)) Then
                DataTable.Item(e.ColumnIndex, e.RowIndex).Value = 5
                DataTable.Item(e.ColumnIndex, e.RowIndex).Style.BackColor = Color.Red
            End If
        End If
    End Sub
0

Nu-uh, sorry.

I'm posting my code every time I change something.

    Private Sub DataTable_CellChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataTable.CellValueChanged
        If (e.ColumnIndex = 1) Then
            If (IsNumeric(DataTable.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString())) Then
                DataTable.Item(e.ColumnIndex, e.RowIndex).Value = 5
                DataTable.Item(e.ColumnIndex, e.RowIndex).Style.BackColor = Color.Red
            End If
        End If
    End Sub
0

It doesent work, it's the same line,

IsNumeric(DataTable.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString())
0

I've uploaded the project

You can compile it yourself and try that.
The error message is:
An unhandled exception of type 'System.InvalidOperationException' occurred in WindowsApplication4.exe

Additional information: An error occurred creating the form. See Exception.InnerException for details. The error is: Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index

1

got it
you can try add
on error resume next
or a little if then else like this

    1 Private Sub DataTable_CellChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataTable.CellValueChanged
    2   If (e.ColumnIndex = 1) and e.RowIndex>=0 Then
    3       If (IsNumeric(DataTable.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)) Then
    4           DataTable.Item(e.ColumnIndex, e.RowIndex).Value = 5
    5           DataTable.Item(e.ColumnIndex, e.RowIndex).Style.BackColor = Color.Red
    6       End If
    7   End If
    8 End Sub

Edited by ricky.subiantoputra: bad format

Comments
Thank you so much, it actually works! :)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.