Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6976783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:32:29+00:00 2026-05-27T17:32:29+00:00

I have found a solution in MSDN for ListView sorting… But I found the

  • 0

I have found a solution in MSDN for ListView sorting…
But I found the code to be taking too much time to display the ListView properly when a column is clicked…
So I am asking for a solution to speed it up. I have to sort a ListView containing more than 10,000 items. Here is my whole code…


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;
        }

    }
}

Can anyone help me out of this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:32:30+00:00Added an answer on May 27, 2026 at 5:32 pm

    One of the ways that comes in my mind now is use od data-binding and set VirtualMode http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.virtualmode.aspx. Property to true.

    Doing so you wil achieve foliwing:

    • your big custom data data-layer custom management. In other words you make a sort on data and bind it to listview and not sorting listview items.

    • listView.VirtualMode=true; will force listview control to create listview items only for that items that are visible in ui. In other words, if you have 10.000 items in datacollection but on UI I can se only 15 of them, due the window dimension, the rendering and lustviewitem ui artifact creation time will be spend only for 15 and not for 10.000 like it does in your case.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It looks like a problem which could have simple solution, but I haven't found
I have found many solution for my issue but none doesn't work in my
This is something I've pseudo-solved many times and have never quite found a solution
I have searched for hours now and haven't found a solution for my problem.
i have this problem: ms-access could not delete and i found a potential solution
I have found some in the Cappuccino website (vim, textmate and SubEthaEdit), but not
I've searched the Internet and have found some good solutions for teeing STDOUT to
I have looked all around and only found solutions for python 2.6 and earlier,
Most of the ASP.NET MVC paging solutions I have found by googling look like
I have found that my HTML is, to be honest, very clunky. Small, simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.