How to set focus back in DataGridView Cell


H

Hank

Hi,
I am catching the CellValidating event in the DataGridView cell, if
validating fails, I want put focus back to the cell, following is my code(it
doens't work)

private void dataGridView_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
try
{
// code to validate
................
}
catch(Exception)
{
// if caught exception, will set the focus back
dataGridView.SelectedCells[0].DataGridView.EditingControl.Focus();

}

}

I hit the tab or clicking to another cell, my debug code goes to the
validating, however after calling the DatGridView.EditingControl.Focus(), it
still leaves the selected cell?

Please advise how can I set the focus back to the selected cell?

Thanks!

Hank
 
Ad

Advertisements

C

ClayB

Normally, in Cellvalidating, you only have to set e.Cancel = true; to
force the grid to remain on the cell that was being edited. You do not
have to call DatGridView.EditingControl.Focus. Have you tried only
setting e.Cancel = true;.
================
Clay Burch
Syncfusion, Inc.
 
Ad

Advertisements

Joined
Sep 25, 2014
Messages
1
Reaction score
0
Normally, in Cellvalidating, you only have to set e.Cancel = true; to
force the grid to remain on the cell that was being edited. You do not
have to call DatGridView.EditingControl.Focus. Have you tried only
setting e.Cancel = true;.
================
Clay Burch
Syncfusion, Inc.

Thank you so much brother ..you're so mean to me.. i had tried this two days...thanks a lot
 

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