Results 1 to 2 of 2

Thread: Datagridview incorrect column index??

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2004
    Location
    Berkshire, UK
    Posts
    41

    Question 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:
    1. Private Sub AssetDataGridView_DataError(ByVal sender As Object, _
    2.                                           ByVal e As DataGridViewDataErrorEventArgs) Handles AssetDataGridView.DataError
    3.  
    4.     '  Get column name and header text from sender
    5.  
    6.     Dim colName As String = CType(sender, DataGridView).Columns(e.ColumnIndex).DataPropertyName
    7.     Dim headerText As String = CType(sender, DataGridView).Columns(e.ColumnIndex).HeaderText
    8.  
    9.     '  Now replace column name with user-friendly header name
    10.     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.

  2. #2
    Junior Member
    Join Date
    Jul 2006
    Posts
    29

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width