Click here to Skip to main content
12,244,103 members (50,066 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#3.0 C#
Hi,
I have a datagrid of one column and many rows. Cell become editable when i click on the cell. i have a stutation in which if i edit the cell value to zero and click on the next cell then leaving cell (0.00) should be remove at that location. But movement i click on next cell cellEditEnd methos gets called and in this method i have written the code to remove the cell. But when it try to remove the cell but code datagrid.rows.RemoveAt(index) then exception throws "Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function".While googling i found many workaround for this case e.g Using beginivoke method with deegate or set focus some aother controls or Disbale the grid before the remove cell. But i didn't got the solution. Exception throws when removing the rows from the grid.

Below is stack trace:

---> System.InvalidOperationException: Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function.
  at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)
  at System.Windows.Forms.DataGridView.OnRemovingRow(Int32 rowIndexDeleted, Point& newCurrentCell, Boolean force)
  at System.Windows.Forms.DataGridViewRowCollection.RemoveAtInternal(Int32 index, Boolean force)
  at System.Windows.Forms.DataGridViewRowCollection.RemoveAt(Int32 index)




MY code is simmilar at below:
 private void grdListItems_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
         if (cellAmt == 0)
          {
           RemoveZeroDollarEntry(e.RowIndex);
          }
         }
And 
 private void RemoveZeroDollarEntry(int rowIndex)
        {
          do something...
           grdListItems.Rows.RemoveAt(rowIndex);
        }

Can any one give the correct solution? please.

For googling i have went to below location

http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/f824fbbf-9d08-4191-98d6-14903801acfc/

http://blogs.dotnetnerds.com/steve/archive/2007/05/17/DataGridView-Reentrant-Call-Nightmare.aspx
Posted 1-Dec-11 20:07pm
Edited 1-Dec-11 20:13pm
v3
Comments
Eduard Lu 2-Dec-11 1:14am
   
EDIT: added "pre,b,a,code" tags
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

I met a problem like that and my solution is using CellValueChanged instead of CellEndEdit.
try it
  Permalink  
v2
Comments
mahendra_v 5-Apr-13 6:57am
   
I have used the CellValueChanged instead of CellEndEdit but throwing "Object Reference not set to an instance of an object" error message
David Thomson 9-Jun-13 18:58pm
   
This worked for me. Many thanks.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Set False to MultiSelect Property of your datagridview.
myDataGridView.MultiSelect=false;
  Permalink  
Comments
shiveshkumar 6-Jun-13 5:04am
   
this solution is not working
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

delete the .exe from debug and run the code again...
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 20 Dec 2014
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100