"DataGridViewComboBoxCell value is not valid" while using the BindingSource class

An easy exception to get when working with the DataGridViewComboBoxCell control is that the value is not valid. There are numerous forums out there trying to solve this and many have the strangest work arounds which I could not follow. This exception gets raised when there is a value in the combo cell that is not contained in the combo box's data source. Most data sources come from a DataSet of a database of some sort. But what if we don't have a DataSet but a BindingSource who's data source is an instance of a class?

The following link at Tim Van Wassenhove's website explains how this gets solved. Basically, first initialize your collection class:

MyCollectionClass mycclass = new MyCollectionClass();

then initialize your binding source:

BindingSource bsource = new BindingSource();

then provide a data source for the DataGridViewComboBoxCell:

Mydgvbox.DataSource = FindValues();

where FindValues is a method similar to:
        private BindingList<string> FindValues()
        {
            BindingList<string> bindingList = new BindingList<string>();
            foreach (Stuff s in CollectedStuff)
            {
                bindingList.Add(s.Description);
            }
            return bindingList;
        }
Then set the data sources as follows:

bsource.DataSource = mycclass;
DataGridView1.DataSource = bsource;
DataGridView1 is a DataGridView defined before anything else. That should do it. The data source of "Mydgvbox" above should contain all the possible values else you'll continue having the exception.

Don't forget:
bsource.ResetBindings(false);
at some point in your code when it is not the DataGridView that changes the collection class.

By Frank Neubecker