Link to home
Start Free TrialLog in
Avatar of ARACK04
ARACK04

asked on

System.InvalidOperationException: reentrant call to SetCurrentCellAddressCore

I have a datagridview which is bound to a List of business objects.  I am catching the CellEndEdit event, and calling the update method in my O/R Mapper.  When I edit a cell and hit enter, it works fine.

Unfortunately, when I edit a cell, and then click on a different cell, I get this error:

System.InvalidOperationException: Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function.



This absolutely has me stumped, and any help would be much appreciated.
Avatar of jweibel
jweibel

I think it is trying to prevent a deadlock in occuring. I think the code you are running in the event handler of the CellEndEdit event somehow calls the setcurrentcell method of the datagrid which results in a deadlock.
This is what happens when you click a cell in the datagrid:
setcurrentcell  ->  cellendedit  ->  (your event handler)  -> setcurrentcell


my suggestion: Use a BindingSource when binding your business objects to the datagrid and listen for the listchanged event of the BindingSource object instead of the CellEndEdit event.
Avatar of ARACK04

ASKER

Is there any way I can cancel the cell click event before it goes through all that?
ASKER CERTIFIED SOLUTION
Avatar of jweibel
jweibel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ARACK04

ASKER

That's ok - thanks!
Avatar of ARACK04

ASKER

looks like the BindindSource works perfectly - I didn't really need help setting it up, I just didn't want to change my application.

great!
Avatar of ARACK04

ASKER

One quick thing - if I'm catching the listchanged event, is it then possible to have the datagridView enter edit mode, on a specific cell?  I tried the following, but it's not working (not doing anything)

        void BS_ListChanged(object sender, ListChangedEventArgs e) {
            bind();
            dataGridView1.CurrentCell = dataGridView1.Rows[2].Cells[2];
            dataGridView1.BeginEdit(false);
        }

It doesn't work because later in the process the currentcell of the datagridview is changed elsewhere, thus overriding your code.
You need to put this code in an event that happens after the datagrid update process has finished. e.g. you could put it in the validated event of the datagridview.