Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-07-06
7
Medium Priority
?
491 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 2000 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

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 12 hours left to enroll

916 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