none
How Do I Trap for a dataGridView ComboBox Error? RRS feed

  • Question

  • Simplified:

     

    dataGridView1[0, 1].Value = "widgets";

     

    This is supose to move the dataGridView's combobox (in column 0) SelectedIndex to the proper row (with "widgets" text), and it works unless no match is found, in which case you get: "DataGridView Combo Box Cell value is not valid" error.

     

    But try{ } / catch{ } does not work for this error. How do I trap for this?

     

    And, when there is no match, how do I set the dataGridView's combobox SelectedIndex to -1 ?

     

    Because there were a great many posts in many forums about this I thought I would pass on how I found my problem.

     

    I was comparing the combobox text with text in the master table. Because I was running out of ideas, I went to the detail table and copied the same text and pasted it in the master table in the row that was giving me the trouble, and the problem went away.

     

    I could not tell by looking at the text that it was different, it looked exactly like every other row text with the same value. But there must have been a whitespace character or something different which caused the error.

     

    Thanks,

     

    Dennis

    Tuesday, July 10, 2007 9:11 PM

All replies

  • The grid has a DataError event that you can trap. You can then set the SelectedIndex there and use e.Cancel to prevent an error appearing to the end user.
    Tuesday, July 10, 2007 9:31 PM
  • Thanks Deborah, I can now show my own error message but  "e.Cancel = true"  does not make any difference. And now if I hover over the dataGridView with the mouse the errors keep coming.

     

    Dennis

     

    Tuesday, July 10, 2007 10:08 PM
  • I don't think you will want to put a messagebox in this event.

     

    The error will continue to occur any time the grid tries to repaint or perform any operation, until the problem is fixed.

     

    If you want to let the user know something is wrong - you may want to put a message into one of the edit events instead.

     

    The only code I normally have in my DataError event is this:

     

    Code Snippet

    ' Cancel this error because it does not allow for an empty combo box selection

    Debug.WriteLine("Error: " & "(" & e.RowIndex & "," & e.ColumnIndex & " )" & e.Exception.Message)

    e.Cancel = True

     

    Tuesday, July 10, 2007 10:20 PM
  • Thanks again.

     

    I new to this. How do I cancel/stop the error messages.

    Tuesday, July 10, 2007 10:30 PM
  • I think what you want to do is trap the error in the control rather than the data grid.

     

    To do that you will want to create a custom class the derives from DropDownList or combobox and then override the data binding method so as to be able to catch the no data found exceptions...

     

    Something like this:

     

    Code Snippet

    public class GracefulDropDownList : System.Web.UI.WebControls.DropDownList

    {

        protected override void PerformDataBinding(System.Collections.IEnumerable dataSource)

        {

            try { base.PerformDataBinding(dataSource); }

            catch (ArgumentOutOfRangeException) { }

        }

    }

     

    Tuesday, July 10, 2007 11:38 PM