亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

ListView的Column排序方法

系統 2133 0

ListView的Column排序是很常見的功能。具體實現的時候,主要是下面幾步:

1、創建兩個類

2、重載ColumnClick方法。

ListView的Column排序方法

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 );
}

}


}

}

ListView的Column排序方法


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产精亚洲视频 | 国产成人a一在线观看 | 日本一级特黄大一片免 | 久久亚洲国产的中文 | 欧美啪 | 色综合久久久久久久久五月性色 | 亚洲综合在线另类色区奇米 | 国产高清国产精品国产k | chinese国产一区二区 | 老子影院无码午夜伦不卡 | 中文一区在线观看 | 亚洲精品中文字幕久久久久久 | 日本三级11k影院在线 | 亚洲狠狠成人综合网 | 香蕉国产在线 | 奇米狠狠干 | 日本视频a | 狠狠色婷婷丁香综合久久韩国 | 国产真实偷乱视频在线观看 | 久热久| 亚洲国产欧美在线观看 | 国产精品亚洲欧美一区麻豆 | 国产精品久久久久久久人热 | 免费看一区二区三区 | 久久亚洲精品成人综合 | 久久久久在线 | 动漫美女h片黄动漫在线观看 | 国产精品免费在线播放 | 久久综合亚洲一区二区三区 | 久久久久久久久一级毛片 | 亚洲美女视频网址 | 四虎影院精品在线观看 | 国产香蕉一区二区在线观看 | 色鬼久久爱综合久久鬼色 | 99热这里只有精品国产动漫 | 男人天堂视频网 | 三级a做爰大乳在线观看 | 性欧美一级毛片在线播放 | 国产精品国语自产拍在线观看 | 久久免 | 色老头老太做爰视频在线观看 |