c# ListView sorting problem

I have a ListView which I can sort by clicking the columns. This works just fine. But I have a method which deletes a listview item and adds it again. After this the item is at the bottom and not at the same position as befor. I want it where it was. I tried it with lsvListView.Sort(); but its allways at the bottom..

I sort the columns with the ListViewColumnSorter class from Microsoft..
Who is Participating?

Improve company productivity with a Business Account.Sign Up

Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
>. This works just fine.
can you show it?

for the reference:
socom1985Author Commented:
I'm sure I'm not getting something..
I have it like in the link for the columns clicks and it works..
but when I add a new list item it should automatically sort after the last sorting criteria..
private ListViewColumnSorter lvwColumnSorter;
this.lstvReports.ListViewItemSorter = lvwColumnSorter;
//Lists for faster filtering
lstvicOnlyErrors = new ListViewGroup();
lstvicAllReports = new ListViewGroup();

Open in new window

socom1985Author Commented:
I think I found the problem. When I click the standard column twice to sort it again then it works...
Just have to find out how to do it without that..
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

socom1985Author Commented:
Somehow the lstvListview.Sort(); method doesn't know the standard sorting.. so when I start the app and I remove and add the ListviewItem it doesn't work. But when I first click a column to sort it, then it works.. Anybody know where to set this?
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
adding a item adds at the end, it does not "sort" automatically at the place according to the sorting.
so, after adding a new item, you have to sort explicitly once according to the sort you want.
socom1985Author Commented:
I just did

            //Standart sort = Column 1
            lvwColumnSorter.SortColumn = 1;
            lvwColumnSorter.Order = SortOrder.Descending;

in the constructor
Guy Hengel [angelIII / a3]Billing EngineerCommented:
that does not sort implicitly.
as I said, after each new insert (or bulk of inserts), you need to call the sort() method explicitly.
socom1985Author Commented:
no It works now because I insert the whole ListViewItemGroup new...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.