views:

2320

answers:

3

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?

+1  A: 

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/

Rulas
+1  A: 

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.

JJO
Thanks for the response. Tweaking your example, we keep track of the colour of the shirt and the colour of the pajamas for the last X days.Multiple colours could be removed/made invalid at once. We currently just reset the listbox to an unselected state with only valid values selectable.The problem is that the user no longer knows WHICH colour was worn on certain days (whereas if we could show invalid entries they could see that they wore red on day 6 and blue on day 12) even though red and blue are no longer valid.
BrianH
The historical aspect of this is very interesting. Is this part of your business logic? If so, you don't want them changing history, either, correct? So you do have a very challenging issue on your hands.I think the problem would be better resolved with a custom column, honestly. Ideally, you'd have access to history as-of a certain date. Then, when the combo box is displayed, you could show only the colors that were active on that date. If you don't have access to history, don't even show a combo box, particularly if it's historical.
JJO
It's not really a history, it just fits the example. Maybe its the shirts you want to wear for the rest of the week. Or a meal plan, but you can't have cereal or pancakes now because the milk has gone bad. Long story short: what was once valid has now been removed. In order to re-plan new options, you ideally need to know what the old options were to replace them. As this is for a business application, you need to know that the old value was A so you can replace it with C, this other old value was B so you can replace it with D except for the 3rd instance, where you'd also want C.
BrianH
A: 

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!

j2associates