Enter DataGrid at correct cell
Posted on 2007-03-30
In VB .NET 2003, I have a DataGrid with five columns. I have noticed that if I tab into the grid from the other controls on the form, the cell highlighted is the one that was last the CurrentCell when I was previously in the DataGrid. That's true even if I've moved to a different set of records, which isn't desirable behavior. (If there are no longer as many rows, focus goes to the first row, same column.) I thought of simply setting the .CurrentCell to the first row, first column in the Enter event handler for the DataGrid, but if a user clicks on a cell to make a change to it, I want that click to be recognized and focus to go immediately to that cell.
A couple of possibilities came to my mind to do this:
1) Change the CurrentCell in the Leave event handler. But I'm finding that if I do this, if I leave by means of clicking on a CheckBox, the CheckBox is changed but then focus returns to the DataGrid. (A second click on the CheckBox will get focus to stay there, but this kind of behavior will drive users nuts.)
2) Distinguish between entering the DataGrid via a tab entry (for which I'd set dg.CurrentCell = New DataGridCell(0, 0)) and via a mouse click. But I don't know how to do this.
If there's another way to do it, I'm open to that as well.