Solved

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

Posted on 2009-07-06
7
481 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

23 Experts available now in Live!

Get 1:1 Help Now