Link to home
Start Free TrialLog in
Avatar of socom1985
socom1985

asked on

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..
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of socom1985
socom1985

ASKER

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();
 
 
 
lstvicAllReports.Items.Add(lviReportItems);
 
lstvReports.Items.Clear();
lstvReports.Items.AddRange(lstvicAllReports.Items);
 
 
lstvReports.Sort();

Open in new window

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..
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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I just did

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


in the constructor
that does not sort implicitly.
as I said, after each new insert (or bulk of inserts), you need to call the sort() method explicitly.
no It works now because I insert the whole ListViewItemGroup new...
Thanks..