.NET ListView column order problem


I have a problem in a form, where I've added columns to a .NET ListView control, in the following order:

A   | B   | C   | D

The display index for columns A-D is 0-3, in that order, yet they display in the wrong order:

A   | B   | D   | C
            ^-----^  these are switched at runtime

Note: Everything looks as I want it at design time.

I guess, but I don't know why, that it is because I added column C to the ListView after I had added column D. I moved it up a notch in the column editor dialog, adjusted the display indices, and checked the creation order in the .Designer.cs file, everything is in order A-D, in that order.

Yet the problem persists.

Also note: This is not just a heading label issue, the columns are swapped around, including their data. The data is added in the order I expect it to be displayed, but the last two columns are swapped.

What else do I need to check to figure out why one of my columns is in the wrong position?

I figured out the problem. For some reason the DisplayIndex property isn't persisted, even if I set it in the dialog.

If I had completely closed the form, and reopened it in Visual Studio, then it shifted around. Apparently those properties aren't detected by the dialog editor as "changed", and thus the save mechanism doesn't care to save it for me either.

The code that added the columns, was like this:

this.lvResult = new System.Windows.Forms.ListView();
this.colResultId = new System.Windows.Forms.ColumnHeader();
this.colResultTitle = new System.Windows.Forms.ColumnHeader();
this.colResultLanguage = new System.Windows.Forms.ColumnHeader();
this.colResultTags = new System.Windows.Forms.ColumnHeader();
// lvResult
this.lvResult.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
this.lvResult.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.lvResult.FullRowSelect = true;
this.lvResult.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
this.lvResult.HideSelection = false;
this.lvResult.Location = new System.Drawing.Point(12, 6);
this.lvResult.Name = "lvResult";
this.lvResult.Size = new System.Drawing.Size(466, 117);
this.lvResult.TabIndex = 0;
this.lvResult.UseCompatibleStateImageBehavior = false;
this.lvResult.View = System.Windows.Forms.View.Details;
this.lvResult.SelectedIndexChanged += new System.EventHandler(this.lvResult_SelectedIndexChanged);
// colResultId
this.colResultId.Text = "#";
this.colResultId.Width = 35;
// colResultTitle
this.colResultTitle.Text = "Title";
this.colResultTitle.Width = 220;
// colResultTags
this.colResultTags.DisplayIndex = 2;
this.colResultTags.Text = "Tags";
this.colResultTags.Width = 100;
// colResultLanguage
this.colResultLanguage.Text = "Language";

When I added the missing properties directly in the file, it worked.

Best Solution

Is it perhaps persisting something to the form's .resx instead of the .designer? I can't imagine why it'd do that, but yeah...

Maybe if all else fails, try deleting the ListView from your form. Then, create a new junk form in your project recreating the ListView on the junk form, test the junk form to make sure you're not getting the weird voodoo behavior, and then copy the ListView from the junk form back to your real form?

Terribly convoluted, I know...