Bound DataGridView with bound ComboBox issue resolved

Bound DataGridView with bound ComboBox issue resolved

Old forum URL: forums.lhotka.net/forums/t/1624.aspx


Mat posted on Sunday, October 29, 2006

I've been tearing my hair out with this one for a short while, and found nothing in the forum archives to help me along, so thought I'd document it for others.

I have a DataGridView, containing a ComboBox roughly following the Project Edit section of the Windows Forms UI chapter of the book.

On closing my dialog, I get the following error dialog, numerous times:

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

All well and good, but what is the cause, and how to fix it!

A good bit of googling reveled a blog entry at http://devauthority.com/blogs/jwooley/archive/2005/12/02/638.aspx. Basically, the grid is still bound to its data source, when the ComboBox's data source is disposed.

Rather than taking the approach of messing with the dispose function, I solved the problem by fiddling with ordering of initialisation of the binding sources. This is in the InitializeComponent function, inside the Form.Designer.cs file.

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.dataGridBindingSource = new System.Windows.Forms.BindingSource(this.components);
            this.comboBoxBindingSource = new System.Windows.Forms.BindingSource(this.components);

            ...

Hope this is useful to someone, should they come across the same problem.

Mat

Cslah replied on Saturday, May 12, 2007

Thanks for the post. I had the same error and tried your solution but it didn't work for me, but helped me solve it! What I ended up doing was loading the combo box binding sources before the data grid binding source. Having it the other way around was causing the crash for me.

Not sure if it matters but my UI is in VB.NET and the library is in C#.

Copyright (c) Marimer LLC