Solved

c# ListView sorting problem

Posted on 2009-07-14
8
438 Views
Last Modified: 2013-12-17
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..
0
Comment
Question by:socom1985
  • 5
  • 3
8 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24846931
>. This works just fine.
can you show it?

for the reference:
http://support.microsoft.com/kb/319401
0
 
LVL 1

Author Comment

by:socom1985
ID: 24846969
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

0
 
LVL 1

Author Comment

by:socom1985
ID: 24847115
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..
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 1

Author Comment

by:socom1985
ID: 24847127
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?
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24847151
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.
0
 
LVL 1

Author Comment

by:socom1985
ID: 24847160
I just did

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


in the constructor
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24847171
that does not sort implicitly.
as I said, after each new insert (or bulk of inserts), you need to call the sort() method explicitly.
0
 
LVL 1

Author Comment

by:socom1985
ID: 24847233
no It works now because I insert the whole ListViewItemGroup new...
Thanks..
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question