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,ColumnClickEventArgse)
{
// CreateaninstanceoftheColHeaderclass.
ColHeaderclickedCol = (ColHeader) this .listView1.Columns[e.Column];
// Settheascendingpropertytosortintheoppositeorder.
clickedCol.ascending = ! clickedCol.ascending;
// Getthenumberofitemsinthelist.
int numItems = this .listView1.Items.Count;
// Turnoffdisplaywhiledataisrepoplulated.
this .listView1.BeginUpdate();
// PopulateanArrayListwithaSortWrapperofeachlistitem.
ArrayListSortArray = new ArrayList();
for ( int i = 0 ;i < numItems;i ++ )
{
SortArray.Add( new SortWrapper( this .listView1.Items[i],e.Column));
}
// SorttheelementsintheArrayListusinganewinstanceoftheSortComparer
// class.Theparametersarethestartingindex,thelengthoftherangetosort,
// andtheIComparerimplementationtouseforcomparingelements.Notethat
// theIComparerimplementation(SortComparer)requiresthesort
// directionforitsconstructor;trueifascending,othwisefalse.
SortArray.Sort( 0 ,SortArray.Count, new SortWrapper.SortComparer(clickedCol.ascending));
// Clearthelist,andrepopulatewiththesorteditems.
this .listView1.Items.Clear();
for ( int i = 0 ;i < numItems;i ++ )
this .listView1.Items.Add(((SortWrapper)SortArray[i]).sortItem);
// Turndisplaybackon.
this .listView1.EndUpdate();
}
private void button1_Click( object sender,EventArgse)
{
this .listView1.View = View.Details;
// AddcolumnsusingtheColHeaderclass.Thefourth
// parameterspecifiestrueforanascendingsortorder.
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 ));
// Addthedata.
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 " } ));
// ConnecttheListView.ColumnClickeventtotheColumnClickeventhandler.
this .listView1.ColumnClick += new ColumnClickEventHandler(listView1_ColumnClick);
}
}
public class ColHeader:ColumnHeader
{
public bool ascending;
public ColHeader( string text, int width,HorizontalAlignmentalign, bool asc)
{
this .Text = text;
this .Width = width;
this .TextAlign = align;
this .ascending = asc;
}
}
class SortWrapper
{
internal ListViewItemsortItem;
internal int sortColumn;
// ASortWrapperrequirestheitemandtheindexoftheclickedcolumn.
public SortWrapper(ListViewItemItem, int iColumn)
{
sortItem = Item;
sortColumn = iColumn;
}
// Textpropertyforgettingthetextofanitem.
public string Text
{
get
{
return sortItem.SubItems[sortColumn].Text;
}
}
// ImplementationoftheIComparer
// interfaceforsortingArrayListitems.
public class SortComparer:IComparer
{
bool ascending;
// Constructorrequiresthesortorder;
// trueifascending,otherwisedescending.
public SortComparer( bool asc)
{
this .ascending = asc;
}
// ImplemnentationoftheIComparer:Compare
// methodforcomparingtwoobjects.
public int Compare( object x, object y)
{
SortWrapperxItem = (SortWrapper)x;
SortWrapperyItem = (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 );
}
}
}
}
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,ColumnClickEventArgse)
{
// CreateaninstanceoftheColHeaderclass.
ColHeaderclickedCol = (ColHeader) this .listView1.Columns[e.Column];
// Settheascendingpropertytosortintheoppositeorder.
clickedCol.ascending = ! clickedCol.ascending;
// Getthenumberofitemsinthelist.
int numItems = this .listView1.Items.Count;
// Turnoffdisplaywhiledataisrepoplulated.
this .listView1.BeginUpdate();
// PopulateanArrayListwithaSortWrapperofeachlistitem.
ArrayListSortArray = new ArrayList();
for ( int i = 0 ;i < numItems;i ++ )
{
SortArray.Add( new SortWrapper( this .listView1.Items[i],e.Column));
}
// SorttheelementsintheArrayListusinganewinstanceoftheSortComparer
// class.Theparametersarethestartingindex,thelengthoftherangetosort,
// andtheIComparerimplementationtouseforcomparingelements.Notethat
// theIComparerimplementation(SortComparer)requiresthesort
// directionforitsconstructor;trueifascending,othwisefalse.
SortArray.Sort( 0 ,SortArray.Count, new SortWrapper.SortComparer(clickedCol.ascending));
// Clearthelist,andrepopulatewiththesorteditems.
this .listView1.Items.Clear();
for ( int i = 0 ;i < numItems;i ++ )
this .listView1.Items.Add(((SortWrapper)SortArray[i]).sortItem);
// Turndisplaybackon.
this .listView1.EndUpdate();
}
private void button1_Click( object sender,EventArgse)
{
this .listView1.View = View.Details;
// AddcolumnsusingtheColHeaderclass.Thefourth
// parameterspecifiestrueforanascendingsortorder.
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 ));
// Addthedata.
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 " } ));
// ConnecttheListView.ColumnClickeventtotheColumnClickeventhandler.
this .listView1.ColumnClick += new ColumnClickEventHandler(listView1_ColumnClick);
}
}
public class ColHeader:ColumnHeader
{
public bool ascending;
public ColHeader( string text, int width,HorizontalAlignmentalign, bool asc)
{
this .Text = text;
this .Width = width;
this .TextAlign = align;
this .ascending = asc;
}
}
class SortWrapper
{
internal ListViewItemsortItem;
internal int sortColumn;
// ASortWrapperrequirestheitemandtheindexoftheclickedcolumn.
public SortWrapper(ListViewItemItem, int iColumn)
{
sortItem = Item;
sortColumn = iColumn;
}
// Textpropertyforgettingthetextofanitem.
public string Text
{
get
{
return sortItem.SubItems[sortColumn].Text;
}
}
// ImplementationoftheIComparer
// interfaceforsortingArrayListitems.
public class SortComparer:IComparer
{
bool ascending;
// Constructorrequiresthesortorder;
// trueifascending,otherwisedescending.
public SortComparer( bool asc)
{
this .ascending = asc;
}
// ImplemnentationoftheIComparer:Compare
// methodforcomparingtwoobjects.
public int Compare( object x, object y)
{
SortWrapperxItem = (SortWrapper)x;
SortWrapperyItem = (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 );
}
}
}
}