refreshing a listctrl

ok here is what I have.  I am opening up fields from a database and displaying them inside of a listctrl.  Now when I select of the fields and make changes to that field, in the database, I want the changes to show up in the listctrl.  Now what I am doing to do that is just reopen the database to show the changes.  Now I want the field that I chose in the listctrl to be in view.  I can't seem to get that to work.  Here is the code that I am using.

int nIndex = nIndex = m_database.GetTopIndex();

                  m_database.SetItemState(nIndex, LVIS_SELECTED, LVIS_SELECTED);
                  m_database.EnsureVisible(nIndex, TRUE);

m_database is my Meber Variable for my listctrl.  Those functions are called when I press a button.  Now when it runs the first time it works but when I do it again it selects a field that is 3 fields above the one that I want selected.  Sometimes it doesn't even work the first time.  Is this a VC error or is it something that I am doing wrong.
Who is Participating?
RONSLOWConnect With a Mentor Commented:
Sounds like something wrong.

The problem appears to be that you are calling GetTopIndex() .. this is just going to give you the topmost line in the list.  The item you last worked with might be anywhere in the list.

EnsureVisible will not scroll the selected item to the top .. only scrolls enough to amke it visible, it could well be at the bottom (or in the middle) of the display.

Instead, find the selected item (easy call) and remember that between calls (stick it in a member variable).

PS: Seeing some of your code might help
BOOL EnsureVisible( int nItem, BOOL bPartialOK );

The list view control is scrolled if necessary. If the bPartialOK parameter is nonzero, no scrolling occurs if the item is partially visible.

so call it like this:
m_database.EnsureVisible(nIndex, 0);
simongodAuthor Commented:
thanks for the help but I figured out the question myself.  It was because I was using the GetTopIndex function.  I changed it to another that works better.  I will give you the points but you really didn't do a thing to help me.  sorry
All Courses

From novice to tech pro — start learning today.