Windows Develop Bookmark and Share   
 index > Windows Forms General > DataGridView.InvalidOperationException
 

DataGridView.InvalidOperationException

I get the following exception about 1 in 100 times. It does nothing to hinder my program at all from what I can see. What I need is the code to help me suppress this message or handle it, I have tried everything I have read in both help and searches and I can't seem to figure it out. So if anyone can help me out thanks in advance. I am using C# in Visual Studio 2005 and this is the error message.
The following exception occurred in your DataGridView:

System.InvalidOperationException: BindingSource cannot be its own data source. Do not set the DataSource and DataMember properties to values that refer back to BindingSource.
at System.Windows.Forms.BindingSource.get_Count()
at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 boundColumnIndex, Int32 columnIndex, Int32 rowIndex)

To replace this default dialog please handle the DataError event.

JCorner  Wednesday, September 09, 2009 8:43 PM
The only thing that seem to be consistently mentioned in the handful of forum discussions on this particular error is that the datagridview is being updated/refreshed on a different thread from the UI thread.

Looking at the BindingSource.Count get accessor method in reflector, the object retains a recursionDetectionFlag. When a thread reads the Count property the flag is set to true. When the count is obtained the flag is set back to false. However, if a second thread tries to the read the property whilst recursionDetection is true the InvalidOperationException is thrown.

From what I can see, there are many databinding operations that may involve a call to BindingSource.Count.

Are multiple threads accessing the datagridview? Have a look at how you are accessing your data grid view.
Wole Ogunremi  Wednesday, September 09, 2009 9:43 PM
There's a couple of useful suggestions here .

If possible let the main UI thread manage updates to the datagrid.
Also, you can wrap the code updating the grid underlying data in the lock statement - then only one thread at a time will update it.

Good luck.
Wole Ogunremi  Thursday, September 10, 2009 7:24 AM
The only thing that seem to be consistently mentioned in the handful of forum discussions on this particular error is that the datagridview is being updated/refreshed on a different thread from the UI thread.

Looking at the BindingSource.Count get accessor method in reflector, the object retains a recursionDetectionFlag. When a thread reads the Count property the flag is set to true. When the count is obtained the flag is set back to false. However, if a second thread tries to the read the property whilst recursionDetection is true the InvalidOperationException is thrown.

From what I can see, there are many databinding operations that may involve a call to BindingSource.Count.

Are multiple threads accessing the datagridview? Have a look at how you are accessing your data grid view.
Wole Ogunremi  Wednesday, September 09, 2009 9:43 PM
In case anyone is interested, this is how the BindingSource.Count property is implemented:

[Browsable(false)]
public virtual int Count
{
    get
    {
        int count;
        try
        {
            if (this.disposedOrFinalized)
            {
                return 0;
            }
            if (this.recursionDetectionFlag)
            {
                throw new InvalidOperationException(SR.GetString("BindingSourceRecursionDetected"));
            }
            this.recursionDetectionFlag = true;
            count = this.List.Count;
        }
        finally
        {
            this.recursionDetectionFlag = false;
        }
        return count;
    }
}
 


Wole Ogunremi  Wednesday, September 09, 2009 9:50 PM
Yes there are. But Why when doing the same operation would this only come up so rarely and inconsistently? If you could lead me on a way to supress this message that would be great.
JCorner  Thursday, September 10, 2009 5:00 AM
There's a couple of useful suggestions here .

If possible let the main UI thread manage updates to the datagrid.
Also, you can wrap the code updating the grid underlying data in the lock statement - then only one thread at a time will update it.

Good luck.
Wole Ogunremi  Thursday, September 10, 2009 7:24 AM

You can use google to search for other answers

Custom Search

More Threads

• [Windows Forms/C#] Help required on Splash screen!!
• Strange issue
• User Control
• Winform TabStrip control
• Saving TreeView nodes
• Add button to toolstrip at runtime (Noob)
• Images in ListView display with strange black outline
• Components don't lose focus...
• Emergence Scanner Needing
• How do I set the background colour of a scrollbar?