Question

I have a Datagridview where users can enter text. The cell must have a valid data on it, when it doesn't he will get a message how it should be and the edited value is canceled, which triggerd in the CellLeave event. When this happens I want the application to stay on this cell, but each time it goes (which the user select with the cursor or by pressing tab) but I want it to stay on the invalid cell. When I search on this problem I get some solution like:

datagridview.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true;

or

datagridview.CurrentCell = datagridview.Rows[e.RowIndex].Cells[e.ColumnIndex];

But no magic happens here. Any ideas what I'm doing wrong?

Était-ce utile?

La solution

Instead of using the CellLeave event, try using the CellValidating event. If you cancel the event, I believe the DataGridView forces the user to amend the data before they can leave the cell.

The given DataGridViewCellValidatingEventArgs object should have the value you need in e.FormattedValue and can be cancelled if e.Cancel is set to true.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top