.net教程:抛砖引玉——实现listview点击column排序的代码,可实现按时间,字符,数字
using system;
using system.componentmodel;
using system.collections;
using system.diagnostics;
using system.windows.forms;
using system.drawing;
namespace listviewcontrol
{
/// <summary>
/// summary description for usercontrol1.
/// </summary>
public class eastspider : system.windows.forms.listview
{
/// <summary>
/// required designer variable.
/// </summary>
///
private comparer mycomparer;
private arraylist coltypes = new arraylist();
private system.componentmodel.container components = null;
/// <summary>
/// sort interface
/// </summary>
public class comparer : icomparer
{
private int column;
private int sign;
private eastspider.columntypeenum coltype;
public comparer()
{
column = 0;
sign = 1;
coltype = eastspider.columntypeenum.columntypetext;
}
public int column
{
set
{
column = value;
}
get
{
return column;
}
}
public int sign
{
set
{
sign = value;
}
get
{
return sign;
}
}
public columntypeenum coltype
{
set
{
coltype = value;
}
get
{
return coltype;
}
}
public int compare(object o1,object o2)
{
listviewitem li1 = (listviewitem)o1;
listviewitem li2 = (listviewitem)o2;
string s1,s2;
try
{
if (column == 0)
{
s1 = li1.text;
s2 = li2.text;
//return sign * string.compare(s1,s2);
}
else
{
s1 = li1.subitems[column].text;
s2 = li2.subitems[column].text;
}
if (s1.length == 0)
{
if (s2.length > 0)
{
return (-sign);
}
else
{
return 0;
}
}
else if (s2.length == 0)
{
return sign;
}
switch (coltype)
{
case columntypeenum.columntypenumber:
long d1 = long.parse(s1);
long d2 = long.parse(s2);
if (d1 < d2)
{
return (-sign);
}
else if (d1 > d2)
{
return sign;
}
else
{
return 0;
}
case columntypeenum.columntypedate:
datetime t1 = datetime.parse(s1);
datetime t2 = datetime.parse(s2);
return sign*datetime.compare(t1,t2);
default:
return sign * string.compare(s1,s2);
}
}
catch
{
return (0);
}
}
}
/// <summary>
/// 排序类型
/// </summary>
[category("sorttype"),description("the sort type of column you wanted do")]
public enum columntypeenum
{
columntypetext = 0,
columntypedate = 1,
columntypenumber = 2
}
/// <summary>
/// clear all items and columns
/// </summary>
private void clearall()
{
clear();
clearcolumns();
}
/// <summary>
/// clear all item
/// </summary>
private new void clear()
{
base.items.clear();
}
/// <summary>
/// clear all columns and coltype
/// </summary>
private void clearcolumns()
{
base.columns.clear();
coltypes.clear();
}
public void addcolumn(string caption,columntypeenum columntype,horizontalalignment ha,int width)
{
columnheader ch = new columnheader();
ch.text = caption;
ch.textalign = ha;
ch.width = width;
base.columns.add(ch);
coltypes.add(columntype);
}
/// <summary>
/// add items
/// </summary>
/// <param name="item"></param>
/// <param name="bgcolor"></param>
/// <returns></returns>
public int addrow(listviewitem item,color bgcolor)
{
//listviewitem li = new listviewitem(args);
item.useitemstyleforsubitems = true;
item.backcolor = bgcolor;
items.add(item);
return item.index;
}
/// <summary>
/// 点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ev_columnclick(object sender,system.windows.forms.columnclickeventargs e)
{
//messagebox.show(e.column.tostring());
mycomparer.column = e.column;
mycomparer.sign = (base.sorting == sortorder.ascending)? -1:1;
mycomparer.coltype = (columntypeenum)coltypes[e.column];
base.beginupdate();
switch (base.sorting)
{
case sortorder.ascending:
base.sorting = sortorder.descending;
break;
case sortorder.descending:
base.sorting = sortorder.ascending;
break;
}
base.endupdate();
}
/// <summary>
/// override onclick
/// </summary>
/// <param name="e"></param>
protected override void onclick( system.eventargs e)
{
base.onclick(e);
}
/// <summary>
/// override onmousedown
/// </summary>
/// <param name="e"></param>
protected override void onmousedown( system.windows.forms.mouseeventargs e)
{
onclick(e);
}
/// <summary>
/// initializecomponent
/// </summary>
public eastspider()
{
// this call is required by the windows.forms form designer.
base.columnclick += new system.windows.forms.columnclickeventhandler(this.ev_columnclick);
mycomparer = new comparer();
base.listviewitemsorter = (icomparer)mycomparer;
//initializecomponent();
// todo: add any initialization after the initform call
}
/// <summary>
/// clean up any resources being used.
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
//if (components != null)
//{
// components.dispose();
/