ListView.Sorting : ListView « System.Windows.Forms « C# / C Sharp by API

Home
C# / C Sharp by API
1.Microsoft.Win32
2.System
3.System.Collections
4.System.Collections.Generic
5.System.Collections.Specialized
6.System.ComponentModel
7.System.Configuration
8.System.Data
9.System.Data.Common
10.System.Data.Linq
11.System.Data.Odbc
12.System.Data.OleDb
13.System.Data.Sql
14.System.Data.SqlClient
15.System.Diagnostics
16.System.DirectoryServices
17.System.Drawing
18.System.Drawing.Drawing2D
19.System.Drawing.Imaging
20.System.Drawing.Printing
21.System.Drawing.Text
22.System.EnterpriseServices
23.System.Globalization
24.System.IO
25.System.IO.Compression
26.System.IO.IsolatedStorage
27.System.IO.Ports
28.System.Linq
29.System.Management
30.System.Media
31.System.Messaging
32.System.Net
33.System.Net.Mail
34.System.Net.NetworkInformation
35.System.Net.Sockets
36.System.Reflection
37.System.Resources
38.System.Runtime
39.System.Runtime.CompilerServices
40.System.Runtime.InteropServices
41.System.Runtime.Remoting
42.System.Runtime.Remoting.Channels
43.System.Runtime.Remoting.Channels.Http
44.System.Runtime.Remoting.Messaging
45.System.Runtime.Serialization
46.System.Runtime.Serialization.Formatters.Binary
47.System.Runtime.Serialization.Formatters.Soap
48.System.Security
49.System.Security.AccessControl
50.System.Security.Cryptography
51.System.Security.Cryptography.X509Certificates
52.System.Security.Permissions
53.System.Security.Policy
54.System.Security.Principal
55.System.ServiceProcess
56.System.Text
57.System.Text.RegularExpressions
58.System.Threading
59.System.Timers
60.System.Web.Security
61.System.Web.Services
62.System.Windows.Controls
63.System.Windows.Forms
64.System.Xml
65.System.Xml.Linq
66.System.Xml.Schema
67.System.Xml.Serialization
68.System.Xml.XPath
69.System.Xml.Xsl
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp by API » System.Windows.Forms » ListView 
ListView.Sorting
 


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.ListView listView1;
        private System.Windows.Forms.ColumnHeader lvcState;
        private System.Windows.Forms.ColumnHeader lvcCapital;

        public Form1()
        {
            InitializeComponent();
            listView1.Items.Add (new ListViewItem (new string[]{"a","d"}));
            listView1.Items.Add (new ListViewItem (new string[]{"b","c"}));
            listView1.Items.Add (new ListViewItem (new string[]{"c","b"}));
            listView1.Items.Add (new ListViewItem (new string[]{"d","a"}));
            
            listView1.View =  View.Details;
        }
        private void InitializeComponent()
        {
            this.listView1 = new System.Windows.Forms.ListView();
            this.lvcState = new System.Windows.Forms.ColumnHeader();
            this.lvcCapital = new System.Windows.Forms.ColumnHeader();
            this.SuspendLayout();

            this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
                                                                                        this.lvcState,
                                                                                        this.lvcCapital});
            this.listView1.FullRowSelect = true;
            this.listView1.Location = new System.Drawing.Point(032);
            this.listView1.MultiSelect = false;
            this.listView1.Name = "listView1";
            this.listView1.Size = new System.Drawing.Size(292160);
            this.listView1.Sorting = System.Windows.Forms.SortOrder.Descending;
            this.listView1.TabIndex = 3;
            this.listView1.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.listView1_OnColumnClick);
            this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_OnSelectedIndexChanged);
            // 
            // lvcState
            // 
            this.lvcState.Text = "State Name";
            this.lvcState.Width = 120;
            // 
            // lvcCapital
            // 
            this.lvcCapital.Text = "Capital City";
            this.lvcCapital.Width = 120;
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(513);
            this.ClientSize = new System.Drawing.Size(292273);
            this.Controls.AddRange(new System.Windows.Forms.Control[] { this.listView1,
                                                                       });
            this.MinimumSize = new System.Drawing.Size(300300);
            this.Name = "Form1";
            this.Text = "The State Flags";
            this.ResumeLayout(false);

        }
        static void Main() 
        {
            Application.Run(new Form1());
        }

        private void listView1_OnColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {
            ListViewSorter Sorter = new ListViewSorter();
            listView1.ListViewItemSorter = Sorter;
            if (!(listView1.ListViewItemSorter is ListViewSorter))
                return;
            Sorter = (ListViewSorterlistView1.ListViewItemSorter;

            if (Sorter.LastSort == e.Column)
            {
                if (listView1.Sorting == SortOrder.Ascending)
                    listView1.Sorting = SortOrder.Descending;
                else
                    listView1.Sorting = SortOrder.Ascending;
            }
            else{
                listView1.Sorting = SortOrder.Descending;
            }
            Sorter.ByColumn = e.Column;

            listView1.Sort ();
        }

        private void listView1_OnSelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            string State = listView1.SelectedItems[0].SubItems[0].Text;
            
            string Capital = listView1.SelectedItems[0].SubItems[1].Text;
            if (State.Length > 0)
            {
                Console.WriteLine(State + "   " + Capital);
            }
        }
    }
    
    public class ListViewSorter : System.Collections.IComparer
    {
        public int Compare (object o1, object o2)
        {
            if (!(o1 is ListViewItem))
                return (0);
            if (!(o2 is ListViewItem))
                return (0);

            ListViewItem lvi1 = (ListViewItemo2;
            string str1 = lvi1.SubItems[ByColumn].Text;
            ListViewItem lvi2 = (ListViewItemo1;
            string str2 = lvi2.SubItems[ByColumn].Text;

            int result;
            if (lvi1.ListView.Sorting == SortOrder.Ascending)
                result = String.Compare (str1, str2);
            else
                result = String.Compare (str2, str1);

            LastSort = ByColumn;

            return (result);
        }


        public int ByColumn
        {
            get {return Column;}
            set {Column = value;}
        }
        int Column = 0;

        public int LastSort
        {
            get {return LastColumn;}
            set {LastColumn = value;}
        }
        int LastColumn = 0;
    }   

           

   
  
Related examples in the same category
1.extends ListView
2.ListView.Click
3.ListView.ColumnClick
4.ListView.DragDrop
5.ListView.ItemActivate
6.ListView.Items
7.ListView.LargeImageList
8.ListView.MouseUp
9.ListView.View
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.