可以,将多个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;
}
}
{
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;
}
}