Preventing focus from leaving DataGridView

  • Thread starter Thread starter Larry
  • Start date Start date
L

Larry

Hi,

I need to prevent a user from moving away from a DataGridView if it contains
invalid data.

Some specifics: The user should be able to move within the specific grid,
but not move to any other control on the form (including any other grid on
the form). I have tried a number of methods, for example:
1) From the Leave event of the grid: Set focus back to the grid (with and
without also calling BeginEdit)
2) From the RowValidating, set cancel = true if invalid data
3) From CellValidating, set cancel = true if invalid data

None of the above prevents the user from leaving the grid.

I could get it to work by trapping the Enter event on all other controls on
the form and setting focus back to the grid in question as needed from
there, but there must be a better way than that.

Does anyone know of a cleaner method to accomplish this?

Thanks,
Larry
 
Hi, Larry,
My English is poor. I just write solution for my understanding. If you has
more question. Can discuss with me continus.

If you want control can not leave focus, you should handle Validating
event to set e.Cancel to true.
So, user can move current cell in this dataGridView. but he can not set
the focus to other controls. And the application will cannot to close. :)
 
What about capturing the Validating event and restoring focus if the data
is not valid?

Tried that - does not work (but you'd think it would - not sure why it does
not). Seems like an oversight in the design of the grid that this should be
so hard...

Larry
 
Back
Top