helloyesyes 发表于 2013-1-28 11:56:15

ListView的Column排序方法

ListView的Column排序是很常见的功能。具体实现的时候,主要是下面几步:
1、创建两个类
2、重载ColumnClick方法。
http://p.blog.csdn.net/images/p_blog_csdn_net/blog51/ListViewSort.jpg
<div style="padding-right: 5.4pt; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; width: 95%; padding-top: 4px;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.Collections.Generic;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.ComponentModel;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.Data;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.Drawing;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.Text;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.Windows.Forms;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.Collections;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifnamespacetest
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicpartialclassForm1:Form
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicForm1()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifInitializeComponent();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifprivatevoidlistView1_ColumnClick(objectsender,ColumnClickEventArgse)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//CreateaninstanceoftheColHeaderclass.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifColHeaderclickedCol=(ColHeader)this.listView1.Columns;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//Settheascendingpropertytosortintheoppositeorder.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifclickedCol.ascending=!clickedCol.ascending;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//Getthenumberofitemsinthelist.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifintnumItems=this.listView1.Items.Count;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//Turnoffdisplaywhiledataisrepoplulated.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.listView1.BeginUpdate();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//PopulateanArrayListwithaSortWrapperofeachlistitem.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifArrayListSortArray=newArrayList();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.giffor(inti=0;i<numItems;i++)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifSortArray.Add(newSortWrapper(this.listView1.Items,e.Column));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//SorttheelementsintheArrayListusinganewinstanceoftheSortComparer
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//class.Theparametersarethestartingindex,thelengthoftherangetosort,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//andtheIComparerimplementationtouseforcomparingelements.Notethat
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//theIComparerimplementation(SortComparer)requiresthesort
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//directionforitsconstructor;trueifascending,othwisefalse.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifSortArray.Sort(0,SortArray.Count,newSortWrapper.SortComparer(clickedCol.ascending));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//Clearthelist,andrepopulatewiththesorteditems.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.listView1.Items.Clear();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.giffor(inti=0;i<numItems;i++)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.listView1.Items.Add(((SortWrapper)SortArray).sortItem);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//Turndisplaybackon.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.listView1.EndUpdate();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifprivatevoidbutton1_Click(objectsender,EventArgse)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.listView1.View=View.Details;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//AddcolumnsusingtheColHeaderclass.Thefourth
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//parameterspecifiestrueforanascendingsortorder.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.giflistView1.Columns.Add(newColHeader("Name",110,HorizontalAlignment.Left,true));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.giflistView1.Columns.Add(newColHeader("Region",50,HorizontalAlignment.Left,true));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.giflistView1.Columns.Add(newColHeader("Sales",70,HorizontalAlignment.Left,true));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//Addthedata.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.giflistView1.Items.Add(newListViewItem(newstring[]...{"Archer,Karen","4","0521.28"}));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.giflistView1.Items.Add(newListViewItem(newstring[]...{"Benson,Max","8","0828.54"}));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.giflistView1.Items.Add(newListViewItem(newstring[]...{"Bezio,Marin","3","0535.22"}));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.giflistView1.Items.Add(newListViewItem(newstring[]...{"Higa,Sidney","2","0987.50"}));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.giflistView1.Items.Add(newListViewItem(newstring[]...{"Martin,Linda","6","1122.12"}));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.giflistView1.Items.Add(newListViewItem(newstring[]...{"Nash,Mike","7","1030.11"}));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.giflistView1.Items.Add(newListViewItem(newstring[]...{"Sanchez,Ken","1","0958.78"}));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.giflistView1.Items.Add(newListViewItem(newstring[]...{"Smith,Ben","5","0763.25"}));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//ConnecttheListView.ColumnClickeventtotheColumnClickeventhandler.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.listView1.ColumnClick+=newColumnClickEventHandler(listView1_ColumnClick);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicclassColHeader:ColumnHeader
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicboolascending;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicColHeader(stringtext,intwidth,HorizontalAlignmentalign,boolasc)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.Text=text;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.Width=width;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.TextAlign=align;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.ascending=asc;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifclassSortWrapper
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifinternalListViewItemsortItem;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifinternalintsortColumn;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//ASortWrapperrequirestheitemandtheindexoftheclickedcolumn.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicSortWrapper(ListViewItemItem,intiColumn)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifsortItem=Item;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifsortColumn=iColumn;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//Textpropertyforgettingthetextofanitem.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicstringText
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifget
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifreturnsortItem.SubItems.Text;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//ImplementationoftheIComparer
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//interfaceforsortingArrayListitems.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicclassSortComparer:IComparer
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifboolascending;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//Constructorrequiresthesortorder;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//trueifascending,otherwisedescending.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicSortComparer(boolasc)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.ascending=asc;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//ImplemnentationoftheIComparer:Compare
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//methodforcomparingtwoobjects.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicintCompare(objectx,objecty)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifSortWrapperxItem=(SortWrapper)x;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifSortWrapperyItem=(SortWrapper)y;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstringxText=xItem.sortItem.SubItems.Text;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstringyText=yItem.sortItem.SubItems.Text;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifreturnxText.CompareTo(yText)*(this.ascending?1:-1);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
页: [1]
查看完整版本: ListView的Column排序方法