Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch DataGridView validation Error when I write value in editable cells?

I've seen a lot of solutions for this where data binding is involved, but I don't have a data source. In this case the combo cell only applies to 1 row (other rows don't have the DataGridViewComboBoxCell).

I set a DataGridViewComboCell up like this:

DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell();
cell.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
cell.Items.AddRange(items.ToArray());   // items is List<string>

And I dynamically re-populate it later like this:

_cell.Items.Clear();
_cell.Items.AddRange(this.Data.ResponseOptions.Select( d => d.Description).ToArray());   
//d.Description is of type string

But then I get this nasty dialog that says:

The following exception occurred in the DataGridView: System.ArgumentException: DataGridViewComboBoxCell value is not valid. To replace this default dialog please handle the DataError event.

It doesn't help much, by the way, to say it is not "valid". Is it fair to send an email to MS saying Windows Forms is not valid?

I've tried grabbing the cell's items property and adding the strings using a foreach() with an Add() call. I still get the dialog.

I've also tried blowing away the entire cell everytime I want to update it and recreating a new DataGridViewComboCell from scratch. I still get the dialog.

I've also tried manually overwriting the columns value (succeeds when I don't have this problem). Didn't fix it, though.

I only seem to get this dialog when I'm trying to repopulate the items in the combo cell.

For now I just wiped out the DataError method.

Any suggestions?

like image 882
micahhoover Avatar asked Oct 21 '13 19:10

micahhoover


1 Answers

This question is a bit old but I ran into this and it turns out that ThunderGR absolutely had it right. When I had DataError print a report I began to look at the values in the database and noticed that they actually were in all CAPS. Simply changed my string values to all caps and no longer received the error.

Here is a way of possibly handling the DataError although I'd rather not have it fired in the first place.

    private void dataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
        try
        {
            if (e.Exception.Message == "DataGridViewComboBoxCell value is not valid.")
            {
                object value = dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                if (!((DataGridViewComboBoxColumn)dataGridView.Columns[e.ColumnIndex]).Items.Contains(value))
                {
                    ((DataGridViewComboBoxColumn)dataGridView.Columns[e.ColumnIndex]).Items.Add(value);
                }
            }

            throw e.Exception;
        }
        catch (Exception ex)
        {
            bool rethrow = ExceptionPolicy.HandleException(ex, "UI Policy");
            if (rethrow)
            {
                MessageBox.Show(string.Format(@"Failed to bind ComboBox. "
                + "Please contact support with this message:"
                + "\n\n" + ex.Message));
            }
        }
    }

This way the value will get added to the items collection but still throw the exception where it can be logged. I used a messagebox because I still think it is important that the user experience the error message and contact support to have it fixed. Hope this helps someone!

like image 185
waltmagic Avatar answered Oct 11 '22 09:10

waltmagic