Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > ComboBox Error in DataGridView
 

ComboBox Error in DataGridView

I have a bound DataGridView. A few of the columns are ComboBoxes bound to db lookup tables. They work great as far as displaying/selecting fromthe drop-down data and saving the changes back to the main database table. However, when I close the form I get this error infinite times until I stop debugging:

The following exception occurred in the DataGridView:

System.ArgumentException: DataGridViewComboxCell value is not valid.

To replace this default dialog please handle the DataError event.

Previously, the problem occurred apparently due to AutoSizeColumnsMode being set to AllCells. When I set the property back to "none," the error went away. A blogger instructed me to use the following code in the FormClosing event:

DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None

With this code I could set the AutoSizeColumnsMode to AllCells, and the error went away.

But now any design changes I make to the DataGridView bring the error back when I close the form. Even after I reverse the change. I just have to scrap the form and start over. For example, setting the DataGridView property AlternatingRowsDefaultCellStyle to a color brings the error back. The error persists even after I change the property back.

What do I do????

pencilneck

pencilneck  Tuesday, November 13, 2007 8:58 PM
pencilneck wrote:

...

What do I do????



  1. Most of the time you get this error because the table which fills the comboboxes is unable to match the values in the datagridview. Verify for example that this bindingsource is disposed after the dataGridView.
    You might have to take a look in the .designer.cs file...
  2. handle the DataError event to put error marks next to the cells giving the error, you won't get an messagebox if you do this
    DataGridView view = (DataGridView)sender;
    view.Rows[e.RowIndex].ErrorText = "an error";
    view.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "an error";

    e.ThrowException = false;

DamPee  Wednesday, November 14, 2007 1:42 PM

How do I verify that the BindingSource is disposed after the DataGridView? The only code I have found that seems related is auto-generated code in the form Partial Class of Form1.Designer.vb:

'Form overrides dispose to clean up the component list.

<System.Diagnostics.DebuggerNonUserCode()> _

Protected Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing AndAlso components IsNot Nothing Then

components.Dispose()

End If

MyBase.Dispose(disposing)

End Sub

pencilneck
pencilneck  Wednesday, November 14, 2007 5:30 PM
I am not sure, but i think you need to verify that the objects are loaded in a proper way.
DamPee  Wednesday, November 14, 2007 11:22 PM

Hiï¼?/span> first, I recommend you to check the type in datasource and the datatype in valuemember. It is probably related to the data type problem.

And if you have any further question, please paste more code.

Best Regards

Gavin

Gavin Jin - MSFT  Monday, November 19, 2007 3:10 AM

I checked the datatypes. That has caused the error in other apps that I've had to debug, but not this one.

The ValueMember from the lookup table and the DataPropertyName from the main table are both char(1).

Any other suggestions?

pencilneck

pencilneck  Wednesday, November 21, 2007 8:55 PM
try to move the instantiation of that binding source to the top of InitializeComponent(), after

this.components = new System.ComponentModel.Container(); of course

I think the disposal starts by the end of the container

Vitor Tomaz  Friday, February 20, 2009 3:44 PM
I had the same problem. Moving the instantiation of the binding source (as suggested by Vitor Tomazabove) solved the problem for me.

Thanks,
Magnus
Magnus Öhman  Wednesday, July 01, 2009 8:36 AM
I encountered this same error. It only occurs if you have a combo box in a DataGridView andAllCells is set. It only failsin the Dispose method.

In summary, as was suggested, the problem occurs due to the order that things are Disposed.

Essentially, you need to ensure that your BindingSource is disposed after the DataGridView.

Some folks suggested changing the "order" that variables are initialized, but I don't recommend this because the designer "might" override these settings.

I have simply learned to live with the fact; I ignore the DataError events.

Personally, I consider this a bug in Windows Forms. Of MS will probably say it isn't their problem. Considering they want binding to work out-of-the-box and we as developers should expect things to work "intuitively" this is something they should definitely attempt to remedy.
Jehugaleahsa  Thursday, August 27, 2009 4:39 PM

You can use google to search for other answers

Custom Search

More Threads

• Way to "find" which Form control is bound to a specific property?
• GridView, BusinessObject binding and Null
• Dataset Default Property
• Arrangement Algorithm using System.Drawing classes
• Filling an Array from a DataSet
• Programmatically scroll a DataGridView control horizontally
• VB to Mysql
• Dynamic textbox formatting with databinding IDataErrorInfo feedback
• Very simple, but two problems...
• Unable to add ubound image columns to datagridview