如何更快地排序列表视图
本文关键字:列表 视图 排序 何更快 | 更新日期: 2023-09-27 18:18:59
我在MSDN中找到了ListView排序的解决方案…但是我发现代码要花太多的时间来显示ListView正确时,一列被点击…所以我要求一个加快速度的解决方案。我必须对包含超过10,000项的ListView进行排序。这是我的全部代码…
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Runtime.InteropServices;
namespace ListViewSorter
{
class ListViewSorter
{
const Int32 HDF_SORTDOWN = 0x200;
const Int32 HDF_SORTUP = 0x400;
const Int32 HDI_FORMAT = 0x4;
const Int32 HDM_GETITEM = 0x120b;
const Int32 HDM_SETITEM = 0x120c;
const Int32 LVM_GETHEADER = 0x101f;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, EntryPoint = "SendMessage")]
static extern IntPtr SendMessageLVCOLUMN(IntPtr hWnd, UInt32 msg, IntPtr wParam, ref LVCOLUMN lParam);
[StructLayout(LayoutKind.Sequential)]
public struct LVCOLUMN
{
public Int32 mask;
public Int32 cx;
[MarshalAs(UnmanagedType.LPTStr)]
public string pszText;
public IntPtr hbm;
public Int32 cchTextMax;
public Int32 fmt;
public Int32 iSubItem;
public Int32 iImage;
public Int32 iOrder;
}
public void SetSortIcon( ListView listview, int ColumnIndex)
{
IntPtr clmHdr = SendMessage(listview.Handle, LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero);
SortOrder sorting = listview.Sorting;
for (int i = 0; i < listview.Columns.Count; i++)
{
IntPtr clmPtr = new IntPtr(i);
LVCOLUMN lvColumn = new LVCOLUMN();
lvColumn.mask = HDI_FORMAT;
SendMessageLVCOLUMN(clmHdr, HDM_GETITEM, clmPtr, ref lvColumn);
if (sorting != SortOrder.None && i == ColumnIndex)
{
if (sorting == SortOrder.Ascending)
{
lvColumn.fmt &= ~HDF_SORTDOWN;
lvColumn.fmt |= HDF_SORTUP;
}
else
{
lvColumn.fmt &= ~HDF_SORTUP;
lvColumn.fmt |= HDF_SORTDOWN;
}
}
else
{
lvColumn.fmt &= ~HDF_SORTDOWN & ~HDF_SORTUP;
}
SendMessageLVCOLUMN(clmHdr, HDM_SETITEM, clmPtr, ref lvColumn);
}
}
public int SortColumn(ListView listview, int ColumnIndex, int sortColumn)
{
if (ColumnIndex != sortColumn)
{
sortColumn = ColumnIndex;
listview.Sorting = SortOrder.Ascending;
}
else
{
if (listview.Sorting == SortOrder.Ascending)
listview.Sorting = SortOrder.Descending;
else
listview.Sorting = SortOrder.Ascending;
}
SetSortIcon(listview, sortColumn);
listview.Sort();
listview.ListViewItemSorter = new ListViewItemComparer(ColumnIndex,
listview.Sorting);
return sortColumn;
}
}
class ListViewItemComparer : 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;
try
{
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);
}
catch
{
returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text,
((ListViewItem)y).SubItems[col].Text);
}
if (order == SortOrder.Descending)
returnVal *= -1;
return returnVal;
}
}
}
有谁能帮我解决这个问题吗?
我现在想到的一种方法是使用od data-binding
并设置VirtualMode
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.virtualmode.aspx。属性为true
。
这样做,您将实现以下目标:
-
您的大自定义数据
data-layer
自定义管理。换句话说,您对数据进行排序并将其绑定到listview
而不排序listview
项。 -
listView.VirtualMode=true;
将强制listview
控件仅为ui中可见的项创建listview项。换句话说,如果你在datacollection
中有10,000个项目,但在UI上我只能看到其中的15个,由于窗口尺寸,渲染和lustviewitem UI工件创建时间将只花在15上,而不是像你的情况那样花在10,000上。
我认为你应该尝试定义数据类型,而不是使用对象。强制编译器找出dt需要额外的时间。