문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top