BrianH
BrianH

Reputation: 1080

Display an invalid default value in a DataGridViewComboBoxColumn

I have a DataGridViewComboBoxColumn in a DataGridView in a windows application. The user can change settings elsewhere to potentially invalidate a selection in a DataGridViewComboBoxColumn. I have a requirement to retain/display the invalid item while only leaving valid items selectable in the list.

Without correcting the selection an exception is thrown:
DataGridViewComboBoxCell value is not valid.

Catching and ignoring the setting reverts the selected value to the first valid item in the list.

Is there a way to provide a value to a DataGridViewComboBoxColumn so that it does not show up in the list of selectable values?

Upvotes: 1

Views: 3978

Answers (3)

j2associates
j2associates

Reputation:

Presumably your bound datasource to populate the combobox is read only. If so, why not just temporarily insert the invalid value into the underlying datasource. That way it would still be displayed. You could add a temporary column to flag invalid items and not allow the user to leave it selected and then delete it when the user navigated off of the cell. I've never done this with a datagridview but we did something very similar with a different 3rd party grid. Good Luck!

Upvotes: 0

JJO
JJO

Reputation: 744

I'm going to use an example that the values in the drop-down list are colors, and the DataGridView has a list of t-shirts in your closet.

Have you tried inserting the invalid color into the list object that is bound to the ComboBox column? Perhaps you can insert something into the list whose key matches your invalid color but shows "(Invalid)" (or other similar text). You'd have to respond to the CellValidating event if someone were to try and choose it after your initial binding.

If the invalid colors come by altering a lookup somewhere else in the application so that the DataGridView's values (the t-shirts) are no longer valid, you have some options. Perhaps you could change that logic to look up the data that goes to the list of t-shirts and see if there are any existing t-shirts with that color -- then prompt the user to say "You have deactivated Red, but you have Red T-shirts; what do you want to do?" You could stop deactivating Red, change the T-shirts, or delete the T-shirts.

If the invalid colors come from a source you don't control, you could prompt the user when they try to look at the list of t-shirts, "Red is no longer a valid color for T-shirts, what do we do with the Red t-shirts?"

We have a similar constraint in our application. We dropped the combo boxes and use CellValidating instead.

Upvotes: 1

Rulas
Rulas

Reputation: 1174

I found a very strange solution:

Set the autosizecolumnmode to none...

Here's an explaination

http://www.kebabshopblues.co.uk/2007/03/24/more-on-that-datagridviewcombobox-error/

Upvotes: 1

Related Questions