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

Is this about a DataTable or a DataGridView?

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

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())

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

Try ToString() instead of ToString, just as I wrote it in my previous answer.

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

If it works now, mark the thread as solved. If not, please state clearly what is not working.

It doesent work, it's the same line,

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

Could you give the EXACT error message?

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

Please, could you not have just zipped your project?

try this
If (IsNumeric(DataTable.Rows(e.RowIndex).Cells(1).Value) then

No, the same error occurs. :(

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
commented: Thank you so much, it actually works! :) +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.