Datagridview incorrect column index??
I am trying to write user friendly messages for when data integrity rules are broken in a DataGridView. I have handled the DataError error as per the code below but it does something odd when reading the column index of the DataGridViewDataErrorEventArgs. e.ColumnIndex = 2 but the e.Exception refers to the column at ColumnIndex = 3!
VB Code:
Private Sub AssetDataGridView_DataError(ByVal sender As Object, _
ByVal e As DataGridViewDataErrorEventArgs) Handles AssetDataGridView.DataError
' Get column name and header text from sender
Dim colName As String = CType(sender, DataGridView).Columns(e.ColumnIndex).DataPropertyName
Dim headerText As String = CType(sender, DataGridView).Columns(e.ColumnIndex).HeaderText
' Now replace column name with user-friendly header name
Dim excMessage As String = e.Exception.Message
E.g. ? e.Exception.Message
"Column 'LocationID' does not allow nulls."
but
? colName
"AssetTypeID"
Not sure why this is happening - any help greatly appreciated.
Re: Datagridview incorrect column index??
I see this thread is old, but I just ran across the same issue and figured it out. If anyone else stumbles across this thread:
It seems that e.columnindex returns the index of the currently selected column, not the index of the column that threw the error.