Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Preventing CellEnter in a DataGridView

Status
Not open for further replies.

Silver30

Programmer
Jun 1, 2001
10
0
0
US
I have two unbound DataGridView's on the same form. They are configured in a parent-child arrangement. I want to prevent the navigation to a different row in the parent when there are unsaved changes in the child. In the CellEnter event of the parent, I set the parent's CurrentCell back to the row where there are unsaved children. However, this errors out with a "Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function." message.

Is there any way to prevent users from navigating to any other row in a DataGridView, other than the current row, but allow them to navigate to the outside of the DataGridView (i.e. for Save\Cancel buttons). This scenario seems like it should be fairly common, but I can't find anything on preventing the navigation from row to row, except for the RowValidating event which prevents the navigation to the outside of the grid as well.

Any ideas?
 
you will need some form of Boolean flag that you can "trip" back and forth.

So that when a user modifies a value in a cell you set the flag to true. When the user saves changes you flip the flag over to false. Flipping to false should also occur on some form of "Cancel Changes" button.

Then in the RowFocus or CellEntered event, I can't remember what is actually there, you check to see if the flag is tripped and then if you do not want the row to change to set the event as "handled"

-The answer to your problem may not be the answer to your question.
 
Thanks for your reply.

In other words, you would not prevent the user from moving to another parent row. When the user moves to another parent row, you would either save or discard the changed child rows before the child grid re-syncs with the new parent row (which has to occur when a new parent row becomes current). In this case, the save/discard has to occur in the CellEnter event (msbox), and not with a save/cancel button. The only useability issue I see with this is that the user doesn't have the option of canceling the save/discard operation and return to the previous parent row to continue making changes to the child rows (would result in the same reentrant call error).

Well, I wish they had exposed an e.cancel in the CellEnter event. Thanks again.
 
No, that's not what i said.

you don't cancel the event.

You say e.handled = True

-The answer to your problem may not be the answer to your question.
 
Yeh, read that. But, CellEnter doesn't have an e.handled. So, either I am missing something or you are refering to some other event.

I couldn't cancel the event if I wanted to. If I could, I wouldn't be posting this issue :eek:)
 
Alright, well that is dumb.

So, you are going to need a flag and an integer.
Flag-data changed or not.
Int 1- current row index

in something like cellValidating, you need to compare your row indexes and your flag. then set the e.cancel property to true. This should prevent it from selecting a different cell.

-The answer to your problem may not be the answer to your question.
 
Already been down the "validating" route a while back. Not only did it not allow the user to not move to another parent row (desired affect), but it also didn't allow them to jump out of the parent grid to click a save/cancel button or back into the child grid to make further edits (undesired affect). Basically, the validating event can't "see" where the focus is going... another row or an entirely different control.

So, it looks like I will have to force the user to either save or discard edits on a CellEnter event.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top