一千萬個為什麽

搜索

ListViewColumnSorter在C#中添加listView項時出現性能問題?

有一段時間我一直遇到困難,我的 ListView的填充得非常緩慢(大約15秒左右有3000個條目(有5列)。這讓我很生氣,因為sql查詢返回了值不到第二個。我跟蹤它到 ListViewColumnSorter 是減速。當我註釋掉:

private ListViewColumnSorter lvwColumnSorterKontrahenci = new ListViewColumnSorter();
listViewKontrahenci.ListViewItemSorter = lvwColumnSorterKontrahenci;

ListView got populated within 1 second. I always thought that when i attach column sorter it starts it's job when i actually execute it by clicking on column header.

 private void listViewKontrahenci_ColumnClick(object sender, ColumnClickEventArgs e) {
        ListViewColumnSorter.columnClick(listViewKontrahenci, ref lvwColumnSorterKontrahenci, sender, e, headerIcons);
}

我認為,直到我開始至少一次,這不會是一個問題。

我的listview排序代碼:

    public static void columnClick(ListView varListView, ref ListViewColumnSorter lvwColumnSorter, object sender, ColumnClickEventArgs e, ImageList headerImages) {
        //get list view header
        IntPtr hHeader = SendMessage(varListView.Handle, LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero);
        SendMessage(hHeader, HDM_SETIMAGELIST, IntPtr.Zero, headerImages.Handle);
        SortOrder Order = SortOrder.Descending;
        //format icons
        HDITEM hd = new HDITEM {
                                   mask = HDI_IMAGE | HDI_FORMAT
                               };
        for (int i = 0; i < varListView.Columns.Count; i++) {
           //   if (i != e.Column) {
            hd.fmt = HDF_LEFT | HDF_STRING;
            hd.iImage = -1;
            SendMessage(hHeader, HDM_SETITEM, new IntPtr(i), ref hd);
           //   }
        }
        hd.fmt = HDF_LEFT | HDF_STRING | HDF_BITMAP_ON_RIGHT;
        //ListViewHelper.enableDoubleBuffer(varListView);
        if (Order != SortOrder.None) {
            hd.fmt |= HDF_IMAGE;
        }
        if (e.Column == lvwColumnSorter.SortColumn) {
            if (lvwColumnSorter.Order == SortOrder.Ascending) {
                hd.iImage = 0;
                lvwColumnSorter.Order = SortOrder.Descending;
            } else {
                hd.iImage = 1;
                lvwColumnSorter.Order = SortOrder.Ascending;
            }
        } else {
            hd.iImage = 1;
            lvwColumnSorter.SortColumn = e.Column;
            lvwColumnSorter.Order = SortOrder.Ascending;
        }
        SendMessage(hHeader, HDM_SETITEM, new IntPtr(e.Column), ref hd);
        varListView.Sort();
    }

那你的建議是什麽?每次填充 ListView 時分離/附加分揀機還是有更好的方法?

最佳答案

它會在每次添加項目時對項目進行排序。暫時禁用排序或使用Items.AddRange()方法同時添加一堆項目。並在一次添加一個時考慮Begin/EndUpdate()。

轉載註明原文: ListViewColumnSorter在C#中添加listView項時出現性能問題?