Link to home
Start Free TrialLog in
Avatar of ljcor
ljcorFlag for United States of America

asked on

VB 2008 - Focus on a previously Selected row in a DataGridView

Form1 holds the DataGridView control.  To set focus for the control, in the Form_Activated event I set the CurrentCell as follows:
   Me.EvalItemsDataGridView.CurrentCell = Me.EvalItemsDataGridView(0, 0)
That works fine.  

However, there are times that the user will select a row and then move to another form to do other work on that selected record.  Upon returning to Form1 I want the DataGridView row to be set to that previously selected row.  I do save the Key to that record when it is initially selected.  

For instance:
SavedKey = the selected record ItemID (which is a field in the DataGridControl.)  SavedKey is never modified or replaced while the user works the record on another form and then returns to Form1.

Here is the question:
If SavedKey = 0 then
    Me.EvalItemsDataGridView.CurrentCell = Me.EvalItemsDataGridView(0, 0)
Else
    How can I locate and highlight the SavedKey record when returning to Form1?  
end if
ASKER CERTIFIED SOLUTION
Avatar of RPCIT
RPCIT

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ljcor

ASKER

Ok, but should this type of code go into Form1_Activated?  Or somewhere else?
Avatar of RPCIT
RPCIT

well.. is the form being closed, or just losing focus?
Avatar of ljcor

ASKER

The Form1does not close until the app is closed.
fyi..  form_load happens when the form is .. well... loaded. (once).
form_activate happens everytime it gets focus.. I believe even when you minimize and restore.

if the form is still loaded.. I would put your code "Me.EvalItemsDataGridView.CurrentCell = Me.EvalItemsDataGridView(0, 0)"
in the load..  when the form loses focus, and comes back.. the control should still be on the same cell/row it was on.

if it's closing.. you should put that code (with stuff like what I typed) in the load.

activate is usually only used for stuff you want to happen often.
another thing.. since it's still loaded.. if you are not using modal forms.. than it would be possible for your user to go to form1, and change the control.. effectively making the parent and child forms out of sync.
Avatar of ljcor

ASKER

Thanks for your help on this.