Solved

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

Posted on 2009-07-06
7
480 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
Comment Utility
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
Comment Utility
Ok, but should this type of code go into Form1_Activated?  Or somewhere else?
0
 
LVL 6

Expert Comment

by:RPCIT
Comment Utility
well.. is the form being closed, or just losing focus?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

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

Expert Comment

by:RPCIT
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for your help on this.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now