Preventing focus from leaving DataGridView

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
 
C

Colin Han

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. :)
 
L

Larry

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top