ListView的Column排序方法,按列排序

来源:互联网 发布:单片机初始化函数 编辑:程序博客网 时间:2024/06/05 20:20

ListView的Column排序是很常见的功能。具体实现的时候,主要是下面几步:

1、创建两个类

2、重载ColumnClick方法。

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace test
{
    
public partial class Form1 : Form
    
{
        
public Form1()
        
{
            InitializeComponent();
        }


        
private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
        
{
            
// Create an instance of the ColHeader class.
            ColHeader clickedCol = (ColHeader)this.listView1.Columns[e.Column];

            
// Set the ascending property to sort in the opposite order.
            clickedCol.ascending = !clickedCol.ascending;

            
// Get the number of items in the list.
            int numItems = this.listView1.Items.Count;

            
// Turn off display while data is repoplulated.
            this.listView1.BeginUpdate();

            
// Populate an ArrayList with a SortWrapper of each list item.
            ArrayList SortArray = new ArrayList();
            
for (int i = 0; i < numItems; i++)
            
{
                SortArray.Add(
new SortWrapper(this.listView1.Items[i], e.Column));
            }


            
// Sort the elements in the ArrayList using a new instance of the SortComparer
            
// class. The parameters are the starting index, the length of the range to sort,
            
// and the IComparer implementation to use for comparing elements. Note that
            
// the IComparer implementation (SortComparer) requires the sort
            
// direction for its constructor; true if ascending, othwise false.
            SortArray.Sort(0, SortArray.Count, new SortWrapper.SortComparer(clickedCol.ascending));

            
// Clear the list, and repopulate with the sorted items.
            this.listView1.Items.Clear();
            
for (int i = 0; i < numItems; i++)
                
this.listView1.Items.Add(((SortWrapper)SortArray[i]).sortItem);

            
// Turn display back on.
            this.listView1.EndUpdate();

        }


        
private void button1_Click(object sender, EventArgs e)
        
{
            
this.listView1.View = View.Details;

            
// Add columns using the ColHeader class. The fourth
            
// parameter specifies true for an ascending sort order.
            listView1.Columns.Add(new ColHeader("Name"110, HorizontalAlignment.Left, true));
            listView1.Columns.Add(
new ColHeader("Region"50, HorizontalAlignment.Left, true));
            listView1.Columns.Add(
new ColHeader("Sales"70, HorizontalAlignment.Left, true));

            
// Add the data.
            listView1.Items.Add(new ListViewItem(new string[] "Archer, Karen""4""0521.28" }));
            listView1.Items.Add(
new ListViewItem(new string[] "Benson, Max""8""0828.54" }));
            listView1.Items.Add(
new ListViewItem(new string[] "Bezio, Marin""3""0535.22" }));
            listView1.Items.Add(
new ListViewItem(new string[] "Higa, Sidney""2""0987.50" }));
            listView1.Items.Add(
new ListViewItem(new string[] "Martin, Linda""6""1122.12" }));
            listView1.Items.Add(
new ListViewItem(new string[] "Nash, Mike""7""1030.11" }));
            listView1.Items.Add(
new ListViewItem(new string[] "Sanchez, Ken""1""0958.78" }));
            listView1.Items.Add(
new ListViewItem(new string[] "Smith, Ben""5""0763.25" }));

            
// Connect the ListView.ColumnClick event to the ColumnClick event handler.
            this.listView1.ColumnClick += new ColumnClickEventHandler(listView1_ColumnClick);


        }

    }


    
public class ColHeader : ColumnHeader
    
{
        
public bool ascending;
        
public ColHeader(string text, int width, HorizontalAlignment align, bool asc)
        
{
            
this.Text = text;
            
this.Width = width;
            
this.TextAlign = align;
            
this.ascending = asc;
        }

    }


    
class SortWrapper
    
{
        
internal ListViewItem sortItem;
        
internal int sortColumn;


        
// A SortWrapper requires the item and the index of the clicked column.
        public SortWrapper(ListViewItem Item, int iColumn)
        
{
            sortItem 
= Item;
            sortColumn 
= iColumn;
        }


        
// Text property for getting the text of an item.
        public string Text
        
{
            
get
            
{
                
return sortItem.SubItems[sortColumn].Text;
            }

        }


        
// Implementation of the IComparer
        
// interface for sorting ArrayList items.
        public class SortComparer : IComparer
        
{
            
bool ascending;

            
// Constructor requires the sort order;
            
// true if ascending, otherwise descending.
            public SortComparer(bool asc)
            
{
                
this.ascending = asc;
            }


            
// Implemnentation of the IComparer:Compare
            
// method for comparing two objects.
            public int Compare(object x, object y)
            
{
                SortWrapper xItem 
= (SortWrapper)x;
                SortWrapper yItem 
= (SortWrapper)y;

                
string xText = xItem.sortItem.SubItems[xItem.sortColumn].Text;
                
string yText = yItem.sortItem.SubItems[yItem.sortColumn].Text;
                
return xText.CompareTo(yText) * (this.ascending ? 1 : -1);
            }

        }


    }

}

转自http://blog.csdn.net/blog51

原创粉丝点击