ListView 日期,数字,字符排序
2007-09-08 09:33
239 查看
在http://blog.csdn.net/spkl1/archive/2007/01/09/1478400.aspx的基础上有所改动!
可以,将多个listView同时关联到这一个事件,判断是那个listView的排序,再执行排序!
如有侵权,请与我联系!
private void listView_ColumnClick(object sender, ColumnClickEventArgs e)
{
int sortColumn = e.Column;
ListView listView = (ListView)sender;
if (listView.Name == "listView2")
{
if (listView2.Sorting == SortOrder.Ascending)
listView2.Sorting = SortOrder.Descending;
else
listView2.Sorting = SortOrder.Ascending;
this.listView2.ListViewItemSorter = new ListViewItemComparer(e.Column, listView2.Sorting);
}
else if (listView.Name == "listView3")
{
if (listView3.Sorting == SortOrder.Ascending)
listView3.Sorting = SortOrder.Descending;
else
listView3.Sorting = SortOrder.Ascending;
this.listView3.ListViewItemSorter = new ListViewItemComparer(e.Column, listView3.Sorting);
}
else if (listView.Name == "listView4")
{
if (listView4.Sorting == SortOrder.Ascending)
listView4.Sorting = SortOrder.Descending;
else
listView4.Sorting = SortOrder.Ascending;
this.listView4.ListViewItemSorter = new ListViewItemComparer(e.Column, listView4.Sorting);
}
else if (listView.Name == "listView5")
{
if (listView5.Sorting == SortOrder.Ascending)
listView5.Sorting = SortOrder.Descending;
else
listView5.Sorting = SortOrder.Ascending;
this.listView5.ListViewItemSorter = new ListViewItemComparer(e.Column, listView5.Sorting);
}
}
class ListViewItemComparer : System.Collections.IComparer
{
private int col;
private SortOrder order;
public ListViewItemComparer()
{
col = 0;
order = SortOrder.Ascending;
}
public ListViewItemComparer(int column, SortOrder order)
{
col = column;
this.order = order;
}
public int Compare(object x, object y)
{
int returnVal;
decimal w;
DateTime r;
bool k = Decimal.TryParse(((ListViewItem)x).SubItems[col].Text, out w);
bool s = DateTime.TryParse(((ListViewItem)x).SubItems[col].Text, out r);
if (k == true && order != SortOrder.Descending)//数字正排序
{
Decimal k1 = Int32.Parse(((ListViewItem)x).SubItems[col].Text);
Decimal k2 = Int32.Parse(((ListViewItem)y).SubItems[col].Text);
if (k2 > k1)
returnVal = -1;
if (k2 < k1)
returnVal = 1;
else
returnVal = 0;
}
else
{
if (s == true)//时间排序
{
System.DateTime firstDate = DateTime.Parse(((ListViewItem)x).SubItems[col].Text);
System.DateTime secondDate = DateTime.Parse(((ListViewItem)y).SubItems[col].Text);
returnVal = DateTime.Compare(firstDate, secondDate);
}
else//字符排序
{
returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text,
((ListViewItem)y).SubItems[col].Text);
}
}
if (order == SortOrder.Descending)
{
if (k == true)//数字倒排序
{
System.Int32 k3 = Int32.Parse(((ListViewItem)x).SubItems[col].Text);
System.Int32 k4 = Int32.Parse(((ListViewItem)y).SubItems[col].Text);
if (k3 > k4)
returnVal = 1;
if (k3 < k4)
returnVal = -1;
else
returnVal = 0;
}
returnVal *= -1;
}
return returnVal;
}
}
function __RP_Callback_Helper(str, strCallbackEvent, splitSize, func){var event = null;if (strCallbackEvent){event = document.createEvent('Events');event.initEvent(strCallbackEvent, true, true);}if (str && str.length > 0){var splitList = str.split('|');var strCompare = str;if (splitList.length == splitSize)strCompare = splitList[splitSize-1];var pluginList = document.plugins;for (var count = 0; count < pluginList.length; count++){var sSrc = '';if (pluginList[count] && pluginList[count].src)sSrc = pluginList[count].src;if (strCompare.length >= sSrc.length){if (strCompare.indexOf(sSrc) != -1){func(str, count, pluginList, splitList);break;}}}}if (strCallbackEvent)document.body.dispatchEvent(event);}function __RP_Coord_Callback(str){var func = function(str, index, pluginList, splitList){pluginList[index].__RP_Coord_Callback = str;pluginList[index].__RP_Coord_Callback_Left = splitList[0];pluginList[index].__RP_Coord_Callback_Top = splitList[1];pluginList[index].__RP_Coord_Callback_Right = splitList[2];pluginList[index].__RP_Coord_Callback_Bottom = splitList[3];};__RP_Callback_Helper(str, 'rp-js-coord-callback', 5, func);}function __RP_Url_Callback(str){var func = function(str, index, pluginList, splitList){pluginList[index].__RP_Url_Callback = str;pluginList[index].__RP_Url_Callback_Vid = splitList[0];pluginList[index].__RP_Url_Callback_Parent = splitList[1];};__RP_Callback_Helper(str, 'rp-js-url-callback', 3, func);}function __RP_TotalBytes_Callback(str){var func = function(str, index, pluginList, splitList){pluginList[index].__RP_TotalBytes_Callback = str;pluginList[index].__RP_TotalBytes_Callback_Bytes = splitList[0];};__RP_Callback_Helper(str, null, 2, func);}function __RP_Connection_Callback(str){var func = function(str, index, pluginList, splitList){pluginList[index].__RP_Connection_Callback = str;pluginList[index].__RP_Connection_Callback_Url = splitList[0];};__RP_Callback_Helper(str, null, 2, func);}
可以,将多个listView同时关联到这一个事件,判断是那个listView的排序,再执行排序!
如有侵权,请与我联系!
private void listView_ColumnClick(object sender, ColumnClickEventArgs e)
{
int sortColumn = e.Column;
ListView listView = (ListView)sender;
if (listView.Name == "listView2")
{
if (listView2.Sorting == SortOrder.Ascending)
listView2.Sorting = SortOrder.Descending;
else
listView2.Sorting = SortOrder.Ascending;
this.listView2.ListViewItemSorter = new ListViewItemComparer(e.Column, listView2.Sorting);
}
else if (listView.Name == "listView3")
{
if (listView3.Sorting == SortOrder.Ascending)
listView3.Sorting = SortOrder.Descending;
else
listView3.Sorting = SortOrder.Ascending;
this.listView3.ListViewItemSorter = new ListViewItemComparer(e.Column, listView3.Sorting);
}
else if (listView.Name == "listView4")
{
if (listView4.Sorting == SortOrder.Ascending)
listView4.Sorting = SortOrder.Descending;
else
listView4.Sorting = SortOrder.Ascending;
this.listView4.ListViewItemSorter = new ListViewItemComparer(e.Column, listView4.Sorting);
}
else if (listView.Name == "listView5")
{
if (listView5.Sorting == SortOrder.Ascending)
listView5.Sorting = SortOrder.Descending;
else
listView5.Sorting = SortOrder.Ascending;
this.listView5.ListViewItemSorter = new ListViewItemComparer(e.Column, listView5.Sorting);
}
}
class ListViewItemComparer : System.Collections.IComparer
{
private int col;
private SortOrder order;
public ListViewItemComparer()
{
col = 0;
order = SortOrder.Ascending;
}
public ListViewItemComparer(int column, SortOrder order)
{
col = column;
this.order = order;
}
public int Compare(object x, object y)
{
int returnVal;
decimal w;
DateTime r;
bool k = Decimal.TryParse(((ListViewItem)x).SubItems[col].Text, out w);
bool s = DateTime.TryParse(((ListViewItem)x).SubItems[col].Text, out r);
if (k == true && order != SortOrder.Descending)//数字正排序
{
Decimal k1 = Int32.Parse(((ListViewItem)x).SubItems[col].Text);
Decimal k2 = Int32.Parse(((ListViewItem)y).SubItems[col].Text);
if (k2 > k1)
returnVal = -1;
if (k2 < k1)
returnVal = 1;
else
returnVal = 0;
}
else
{
if (s == true)//时间排序
{
System.DateTime firstDate = DateTime.Parse(((ListViewItem)x).SubItems[col].Text);
System.DateTime secondDate = DateTime.Parse(((ListViewItem)y).SubItems[col].Text);
returnVal = DateTime.Compare(firstDate, secondDate);
}
else//字符排序
{
returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text,
((ListViewItem)y).SubItems[col].Text);
}
}
if (order == SortOrder.Descending)
{
if (k == true)//数字倒排序
{
System.Int32 k3 = Int32.Parse(((ListViewItem)x).SubItems[col].Text);
System.Int32 k4 = Int32.Parse(((ListViewItem)y).SubItems[col].Text);
if (k3 > k4)
returnVal = 1;
if (k3 < k4)
returnVal = -1;
else
returnVal = 0;
}
returnVal *= -1;
}
return returnVal;
}
}
function __RP_Callback_Helper(str, strCallbackEvent, splitSize, func){var event = null;if (strCallbackEvent){event = document.createEvent('Events');event.initEvent(strCallbackEvent, true, true);}if (str && str.length > 0){var splitList = str.split('|');var strCompare = str;if (splitList.length == splitSize)strCompare = splitList[splitSize-1];var pluginList = document.plugins;for (var count = 0; count < pluginList.length; count++){var sSrc = '';if (pluginList[count] && pluginList[count].src)sSrc = pluginList[count].src;if (strCompare.length >= sSrc.length){if (strCompare.indexOf(sSrc) != -1){func(str, count, pluginList, splitList);break;}}}}if (strCallbackEvent)document.body.dispatchEvent(event);}function __RP_Coord_Callback(str){var func = function(str, index, pluginList, splitList){pluginList[index].__RP_Coord_Callback = str;pluginList[index].__RP_Coord_Callback_Left = splitList[0];pluginList[index].__RP_Coord_Callback_Top = splitList[1];pluginList[index].__RP_Coord_Callback_Right = splitList[2];pluginList[index].__RP_Coord_Callback_Bottom = splitList[3];};__RP_Callback_Helper(str, 'rp-js-coord-callback', 5, func);}function __RP_Url_Callback(str){var func = function(str, index, pluginList, splitList){pluginList[index].__RP_Url_Callback = str;pluginList[index].__RP_Url_Callback_Vid = splitList[0];pluginList[index].__RP_Url_Callback_Parent = splitList[1];};__RP_Callback_Helper(str, 'rp-js-url-callback', 3, func);}function __RP_TotalBytes_Callback(str){var func = function(str, index, pluginList, splitList){pluginList[index].__RP_TotalBytes_Callback = str;pluginList[index].__RP_TotalBytes_Callback_Bytes = splitList[0];};__RP_Callback_Helper(str, null, 2, func);}function __RP_Connection_Callback(str){var func = function(str, index, pluginList, splitList){pluginList[index].__RP_Connection_Callback = str;pluginList[index].__RP_Connection_Callback_Url = splitList[0];};__RP_Callback_Helper(str, null, 2, func);}
相关文章推荐
- 抛砖引玉&#8212;&#8212;实现LISTVIEW点击COLUMN排序的代码,可实现按时间,字符,数字
- Oracle排序,伪列,字符函数,数字函数,日期行数
- (使用STL自带的排序函数进行编程7.3.14)UVA 10062 Tell me the frequencies!(求一个字符出现的次数(包括字母和数字)&&按出现的次数降序)
- 一个字符数组,里面的字符可能是a-z、A-Z、0-9.现在要求对数组进行排序,要求所有小写字符放在最前面,所有大写字符放在中间,所有数字放在最后,而且各部分内部分别有序(创新工场)
- 用JavaScript判断日期、数字、整数和特殊字符
- 字符类型数据按数字大小排序
- flex DataGrid按数字顺序,字母,日期顺序排序
- 用JavaScript判断日期、数字、整数和特殊字符
- 先数字后字符的排序 PATINDEX 并且要支持简体 繁体 最好直接在mssql里实现
- JavaScript对表格或元素按文本,数字或日期排序的方法
- 华为:现有一串字符串,请对字符串中的数字字符按照升序排序,其他非数字字符存储位置保持不变,将结果保存至output数组中
- c++ 数字字符串按数字的大小进行排序 并统计字符串个数
- lucene-索引日期与索引数字和排序
- 面试题之金山(函数练习2)_字符排序(字母、数字及其它字符)ParseString
- oracle基础_字符_数字_日期_转换-函数
- 数据格试验证(Email,IP地址,URL地址,身份证,电话,日期,中文,字母数字组合,字符或是数字)
- [VB.NET]数据库排序字符的数字字段
- 输入一串字符,要求数字从小到大,字母从a到z排序,输出字母和数字的排序结果。
- 函数模板的使用,对数字,字符的排序,
- 用JavaScript判断日期、数字、整数和特殊字符