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);
}
}
}
}
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
- ListView的Column排序方法,按列排序
- ListView的Column排序方法
- ListView 的按列排序
- ListView按指定列排序
- Listview排序的方法
- ListView点击列排序
- ListView列排序(c#)
- 一个对HTML table 按column排序的方法
- C# WinForm的ListView的列排序
- 对ListView的列进行排序
- 对listview的列进行排序
- ListView点击列排序的实现
- Extjs 关于列column 排序问题
- C# listView按列排序、listView数据快速载入
- ListView点击列头排序
- ListView点击列头排序
- 对ListView任意列排序
- Datatable的按列排序排序
- 在C#中运用SQLDMO备份和恢复SQLServer数据库
- 如何将ListView中的数据导出到Excel
- 演说之禅:演说中的 1-7-7法则
- 一个可以开机后自动和你打招呼的小程序(希望以后可以成为智能的系统)
- Environment Object
- ListView的Column排序方法,按列排序
- ubuntu 分辨率
- cuteEditor 编码问题(使用前必看)
- C#调用API的一些基础
- FasTime.Data.dll V2.6.5.1 数据库操作组件
- C# 找出一个子串在另一字符串中出现的所有位置
- 从营销角度看软件测试的价值
- 思念
- C#中关于ListView控件的一些操作实现