开发者

C# ListView sorting

开发者 https://www.devze.com 2023-03-09 23:55 出处:网络
I have 2 ListViews and im adding selected items from one to another. First ListView, lets call it availableJobs and the second ListView selectedJobs. Person can select job/jobs from first ListView and

I have 2 ListViews and im adding selected items from one to another. First ListView, lets call it availableJobs and the second ListView selectedJobs. Person can select job/jobs from first ListView and move it to the second one. Now what i would like is that the sorting should stay allso same with as it was in the first ListView. In te first availableJobs the jobs are sorted by the Job ID i just want to keep that sorting allso in the selectedJobs TreeView.

For example:

1)I select Job with ID 3 and move it to the selectedJobs TreeView

2)I select Job with ID 1 and move it to 开发者_JAVA技巧the selectedJobs

Now what happens is that the order in the selectedJobs will be:

Job 3

Job 1

What i need is tht when i add Job with ID 1 it would go in from of the Job with ID 3.

I tried to use a custom Comparer:

    public class IntegerComparer : IComparer
    {
        private int _colIndex;
        public IntegerComparer(int colIndex)
        {
            _colIndex = colIndex;
        }
        public int Compare(object x, object y)
        {
            int nx = int.Parse((x as ListViewItem).SubItems[_colIndex].Text);
            int ny = int.Parse((y as ListViewItem).SubItems[_colIndex].Text);
            return nx.CompareTo(ny);
        }
    }

But the problem with that is that it puts Job with id 10, 11 etc before 2, 3 and etc..


You can use the sorting feature of the ListView:

http://msdn.microsoft.com/en-us/library/ms996467.aspx

or insetad of adding the job straight from the ListView1 to the ListView2 you could populate a list of LineItem(or a tailored object) ordered by Id and you could use that list as DataSource for the ListView2


You can use the custom sorting.To do this, you must implement IComparer class and set ListViewItemSorter property. Following link has a sample implementation:

http://csharpexamples.com/c-sort-listview-items-columns/

0

精彩评论

暂无评论...
验证码 换一张
取 消