views:

61

answers:

1

I have two ListViews in my app, with an initially identical collection of columns. When the user reorders the columns in one, I want the columns in the other to be reordered.

I have the following event handler on one of the views' ColumnReordered event, and the corresponding handler on the other:

private volatile bool reorderingColumns;

private void listView1_ColumnReordered(object sender, ColumnReorderedEventArgs e)
{
    // Prevent reentry - is this necessary?
    if (reorderingColumns)
        return;

    try
    {
        reorderingColumns = true;

        // copy display indices to the other listView
        for (int i = 0; i < columnInfo.Count; i++)
        {
            listView2.Columns[i].DisplayIndex = listView1.Columns[i].DisplayIndex;
        }
    }
    finally
    {
        reorderingColumns = false;
    }
}

however, the order of the columns in the second listview remains unchanged. What do I need to do to get the second listview to redraw its columns in the new order?

+1  A: 

This is because the reordered event fires before the column's display index has actually changed. This will work:

private void listView1_ColumnReordered(object sender, ColumnReorderedEventArgs e)
{
    listView2.Columns[e.Header.Index].DisplayIndex = e.NewDisplayIndex;
}

EDIT: Just to add, you wont need the reorderingColumns check any more with this method.

GenericTypeTea
Is there an event which fires after it changes?
Simon
I don't think so. You could probably implement your own.
GenericTypeTea