Cher
Cher

Reputation: 2947

How to prevent column to be moved in a ListView?

I have columns in a ListView. I allowed them to be moved.

Right now, I want column in position 0 to always remain at that position. So far I only set a simple code which triggers when a column is moved:

Is column the first one?
If yes switch it back with the column it was just swapped.

However, I'd like the move to be prevented, so that column 0 cannot be moved at all. Is there a way to do this? A property like "Column freeze"?

Upvotes: 3

Views: 288

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236308

You can handle ListView.ColumnReordered event. Check whether new or old index equals 0 and cancel column reordering:

private void listView1_ColumnReordered(object sender, ColumnReorderedEventArgs e)
{
    if (e.NewDisplayIndex == 0 || e.OldDisplayIndex == 0)
        e.Cancel = true;
}

Upvotes: 1

Related Questions