Solved

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

Posted on 2009-07-06
7
485 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

732 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