0

When a ListView is in virtual mode, you are responsible for feeding the ListView a ListItem corresponding to index n when it asks through the OnRetrieveItem event.

i sort my list according to my own rules, and tell the listview to repaint:

listView1.Invalidate();

That's fine and dandy.

Except when the user has selected some items. Now when the tree repaints, different items are selected.

What is the technique to sort SelectedIndices?

But if i sort my own personal list

Daniel Pratt
  • 12,007
  • 2
  • 44
  • 61
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219

1 Answers1

1

You'll need to store the selected objects, sort, find the objects by their new indices and reselect them.

The code could look something like this (optimize it as you see fit):

void listView1_ColumnClick( object sender, ColumnClickEventArgs args )
{
    // Store the selected objects
    List<MyDataObject> selectedObjects = new List<MyDataObject>();
    foreach ( int index in listView1.SelectedIndices )
    {
        selectedObjects.Add( m_MyDataObjectsColl[index] );
    }

    // Clear all selected indices
    listView1.SelectedIndices.Clear();

    // Sort the list
    SortListView(listView1, args);

    // Reselect the objects according to their new indices
    foreach ( MyDataObject selectedObject in selectedObjects )
    {
        int index = m_MyDataObjectsColl.FindIndex(
                delegate( MyDataObject obj ) { return obj == selectedObject; }
            );
        listView1.SelectedIndices.Add( index );
    }
}
AVIDeveloper
  • 2,954
  • 1
  • 25
  • 32