源代碼下載:http://www.91files.com/?2AB4T9OBZAZJY5G1BQNL
public class DataListView : ListVIEw
...{
private DataListVIEwSorter columnSort;
public DataListVIEw()
: base()
...{
DoubleBuffered = true;
FullRowSelect = true;
GridLines = true;
HideSelection = false;
MultiSelect = false;
columnSort = new DataListVIEwSorter();
ColumnClick += new ColumnClickEventHandler(DataListVIEw_ColumnClick);
ListVIEwItemSorter = columnSort;
}
/**//// <summary>
/// Return Columns for this list. We hide the original so we can associate
/// a specialised editor with it.
/// </summary>
[Editor(typeof(DataColumnCollectionEditor), typeof(UITypeEditor))]
new public ListVIEw.ColumnHeaderCollection Columns
...{
get
...{
return base.Columns;
}
}
private void DataListVIEw_ColumnClick(object sender, ColumnClickEventArgs e)
...{
DataColumnHeader column = Columns[e.Column] as DataColumnHeader;
if (column == null)
return;
foreach (ColumnHeader columnHeader in Columns)
columnHeader.ImageKey = null;
column.SortOrder = column.SortOrder == SortOrder.Descending ? SortOrder.Ascending : SortOrder.Descending;
&n;columnSort.SortOrder = column.SortOrder;
columnSort.Column = e.Column;
Sort();
}
public bool EndableSorter
...{
get ...{ return !(ListVIEwItemSorter == null); }
set
...{
if (value)
...{
ListVIEwItemSorter = columnSort;
this.HeaderStyle = ColumnHeaderStyle.Clickable;
}
else
...{
ListVIEwItemSorter = null;
HeaderStyle = ColumnHeaderStyle.Nonclickable;
}
}
}
}
internal class DataColumnCollectionEditor : CollectionEditor
...{
public DataColumnCollectionEditor(Type t)
: base(t)
...{
}
protected override Type CreateCollectionItemType()
...{
&nbs return typeof(DataColumnHeader);
}
}
public class DataListVIEwSorter : IComparer
...{
private SortOrder sortOrder;
private int column;
private ListItemComparer numberComparer;
internal DataListVIEwSorter()
...{
numberComparer = new ListItemComparer();
}
public int Compare(object x, object y)
...{
int compareResult;
string first, secondly;
ListViewItem item = x as ListVIEwItem;
first = item == null ? string.Empty : item.SubItems[column].Text;
item = y as ListVIEwItem;
secondly&n item == null ? string.Empty : item.SubItems[column].Text;
compareResult = numberComparer.Compare(first, secondly);
return sortOrder == SortOrder.Ascending ? compareResult : -compareResult;
}
public SortOrder SortOrder
...{
set ...{ sortOrder = value; }
get ...{ return sortOrder; }
}
public int Column
...{
get ...{ return&nbs
set ...{ column = value; }
}
}
public class ListItemComparer : CaseInsensitiveComparer
...{
public ListItemComparer() : base(CultureInfo.InvariantCulture) ...{ }
public new int Compare(object itemX, object itemY)
...{
if (Information.IsDate(itemX) && Information.IsDate(itemY))
return base.Compare(Convert.ToDateTime(itemX), Convert.ToDateTime(itemY));
else if (!Information.IsNumeric(itemX) || !Information.IsNumeric(itemY))
return base.Compare(itemX, itemY);
return base.Compare(
Convert.ToDouble(itemX, CultureInfo.InvariantCulture),
Convert.ToDouble(itemY, CultureInfo.InvariantCulture));
}
}
public class DataColumnHeader : ColumnHeader
...{
/**//// <summary>
/// 構造
/// </summary>
/// <param name="text">列標題名稱</param>
public DataColumnHeader(string text)
: base()
...{
this.Text = text;
}
/**//// <summary>
/// 構造
/// </summary>
public DataColumnHeader() : base() ...{ }
/**//// <summary>
///
/// </summary>
/// <param name="text">列標題名稱</param>
/// <param name="width">列寬</param>
public DataColumnHeader(string text, int width)
: this(text)
...{
this.Width = width;
}
private SortOrder sortOrder;
/**//// <summary>
/// 獲取或設置列當前的排序方式
&nbs /// </summary>
public SortOrder SortOrder
...{
get ...{ return sortOrder; }
set
...{
sortOrder = value;
ImageKey = sortOrder.ToString();
}
}
/**//// <summary>
///
/// </summary>
/// <param name="text">列標題名稱</param>
/// <param name="width">列寬</param>
/// <param name="textAlign">列內容位置</param>
&nb public DataColumnHeader(string text, int width, HorizontalAlignment textAlign)
: this(text, width)
...{
this.TextAlign = textAlign;
}
}
在向ListView添加項時可以先禁用排序功能再進行添加,添加後再啟用排序功能,這樣操作可以優化ListVIEw控件的性能.