views:

894

answers:

1

I have a DataGridView that contains a combo box (DataGridViewComboBoxColumn). This combo box is populated with a set of setup values. These setup values can be inactivated such that only active values are displayed in the combo box, however, existing entries(records) that use the inactive values must still be displayed.

I can successfully loop over the items in the grid and if a record has a value that is no longer active (i.e. part of the DataGridViewComboBoxCell items), I simply add it to the items for that DataGridViewComboBoxCell.

Problem:

I am unable to find a place to put this code such that I do not get the dreaded 'DataGridViewComboBoxCell value is not valid.'

For example, I can put this code into the Paint event of the DataGridView and the grid functions perfectly. No issues with editing/updating values nor do I have any issues in terms of display (inactive value is always shown)... However, it still throws the 'DataGridViewComboBoxCell value is not valid.' error.

Question:

Where can I add code (or how) to add an 'inactive' value after the active values have been added to the combo box (DataGridViewComboBoxColumn), but before the records are bound to avoid getting this error?

+1  A: 

What about just catching the datagridview error and doing nothing with it.

void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
   e.Cancel = true;
}

// Add code in user interface
dataGridView1.DataError += 
    new DataGridViewDataErrorEventHandler(dataGridView1_DataError);
SwDevMan81