Solved

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

Posted on 2009-07-06
7
484 Views
Last Modified: 2012-05-07
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
0
Comment
Question by:ljcor
  • 4
  • 3
7 Comments
 
LVL 6

Accepted Solution

by:
RPCIT earned 500 total points
ID: 24790236
is the savekey located in the data in the grid?  if so... you can do a loop
something like...  it's c# (ish)..  but it should give you the idea.  

foreach(datadridviewrow row in EvalItemsDataGridView.rows)
{
 if(row.cells["cellname"].value == savedkey)
{
 EvalItemsDataGridView.currentcell = row.cells["whichever"];
break;
}
else
{
Me.EvalItemsDataGridView.CurrentCell = Me.EvalItemsDataGridView(0, 0)
}
}
0
 

Author Comment

by:ljcor
ID: 24790259
Ok, but should this type of code go into Form1_Activated?  Or somewhere else?
0
 
LVL 6

Expert Comment

by:RPCIT
ID: 24790370
well.. is the form being closed, or just losing focus?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ljcor
ID: 24790445
The Form1does not close until the app is closed.
0
 
LVL 6

Expert Comment

by:RPCIT
ID: 24790455
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.
0
 
LVL 6

Expert Comment

by:RPCIT
ID: 24790461
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.
0
 

Author Closing Comment

by:ljcor
ID: 31600411
Thanks for your help on this.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question