Boris
Boris

Reputation: 8931

Inserting an item into a C# Winforms listview

The following line gives me a serious headache:

listView1.Items.Insert(0, new ListViewItem("Test", listView1.Groups[0]));

All I want to do is insert an item into a list view. The listview is in detailed mode with groups enabled. The inserted item should go into the first group at index 0. But what happens is that the item is always added as the LAST item in the group. As if the first parameter of Insert(...) had no effect...

Anything I'm missing here?

BTW: Sorting is disabled on the listview!

Upvotes: 3

Views: 19262

Answers (3)

John Henckel
John Henckel

Reputation: 11357

I had this problem also. I'm not using any groups or sorting. Still, if I try to insert at any index, it always show up LAST. I had to create a "sorter" that forces the listview to always use the same order as the Items collection.

class CompareByIndex : IComparer
{
    private readonly ListView _listView;

    public CompareByIndex(ListView listView)
    {
        this._listView = listView;
    }
    public int Compare(object x, object y)
    {
        int i = this._listView.Items.IndexOf((ListViewItem)x);
        int j = this._listView.Items.IndexOf((ListViewItem)y);
        return i - j;
    }
}

and to use it

   this.listView1.ListViewItemSorter = new CompareByIndex(this.listView1);

I wish that I could use a lambda expression instead of a helper class. But I can't figure out how.

Upvotes: 3

user4415282
user4415282

Reputation: 21

Some kind of wizardry,

if you add an item to a list view, and assign group G to the item, the item will be displayed out of place.

Now, if you get the group holding the item (G), change its name to some temporary value, then change back to original name, everything will be displayed OK.

So instead of

listView1.Items.Insert(0, new ListViewItem("Test", listView1.Groups[0]));

do

Dim LVI as new ListViewItem("Test")
listView1.Items.Insert(0, LVI)
LVI.Group = listView1.Groups[0]

Dim TempStr as string = LVI.Group.Header
LVI.Group.Header = "whatever"
LVI.Group.Header = TempStr

Upvotes: 2

H-Man2
H-Man2

Reputation: 3189

You can try:

ListViewItem item = new ListViewItem("Test");
this.listView1.Items.Insert(0, item);
this.listView1.Groups[0].Items.Insert(0, item);

A detailed discussion can be found here.

This example adds three groups to a listview and adds items at the first position of the groups:

for (int groupIndex = 0; groupIndex < 3; ++groupIndex) {
   this.listView1.Groups.Add("GroupKey" + groupIndex, "Test" + groupIndex);

   for (int index = 0; index < 5; ++index) {
      ListViewItem item = new ListViewItem("Test " + groupIndex + "/" + index,
                                           this.listView1.Groups[groupIndex]);
      this.listView1.Items.Insert(0, item);
      this.listView1.Groups[groupIndex].Items.Insert(0, item);
    }
 }

 for (int groupIndex = 2; groupIndex >= 0; --groupIndex) {
    for (int index = 0; index < 5; ++index) {
      ListViewItem item = new ListViewItem("Test2 " + groupIndex + "/" + index,
                                           this.listView1.Groups[groupIndex]);
      this.listView1.Items.Insert(0, item);
      this.listView1.Groups[groupIndex].Items.Insert(0, item);
   }
 }

This is the result: enter image description here

Upvotes: 6

Related Questions