Sorting a ListView

Last post 07-21-2008 4:38 by Bhavesh. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 01-30-2003 11:06

    • ctacke
    • OpenNETCF Staff
    • Top 10 Contributor
    • Joined on 07-27-2007
    • Indiana
    • Posts 1,868

    Sorting a ListView

    Credit: Michael Lipp (Microsoft)

    Here's some sample code that sorts a listview when you click on a column
    header:


    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Collections;

    public class Test : System.Windows.Forms.Form
    {
    ListView lv;

    ListViewItem MakeItem(char c)
    {
    string s = "";
    for (int n = 0; n < 5; n++)
    s += c.ToString();

    ListViewItem item = new ListViewItem(new string[] {
    "1 " + s,
    "2 " + s,
    "3 " + s
    });
    return item;
    }

    protected override void OnLoad(EventArgs e)
    {
    this.Text = "myform";

    this.lv = new ListView();
    this.lv.Parent = this;
    this.lv.Bounds = this.ClientRectangle;
    this.lv.View = View.Details;
    this.lv.ColumnClick += new
    ColumnClickEventHandler(this.lv_ColumnClick);

    for(int n = 0; n < 3; n++)
    lv.Columns.Add(new myColumnHeader("column" + n, 70,
    HorizontalAlignment.Left, true));

    for (int n = 0; n < 10; n++)
    lv.Items.Add(MakeItem((char)('a' + n)));
    }

    public static void Main()
    {
    Application.Run(new Test());
    }

    private void lv_ColumnClick(object sender, ColumnClickEventArgs e)
    {
    myColumnHeader header = (myColumnHeader)this.lv.Columns[e.Column];

    // toggle the ascending property to sort the other direction
    header.Ascending = !header.Ascending;

    int cItems = this.lv.Items.Count;

    // Turn off display while we load data
    this.lv.BeginUpdate();

    ArrayList rgTemp = new ArrayList();

    for (int i = 0; i < cItems; i++)
    rgTemp.Add(new lviWrapper(this.lv.Items[i], e.Column));

    rgTemp.Sort(0, rgTemp.Count, new
    lviWrapper.lviComparer(header.Ascending));

    this.lv.Items.Clear();

    for (int i = 0; i < cItems; i++)
    this.lv.Items.Add(((lviWrapper)rgTemp[i]).m_Item);

    // Turn display back on
    this.lv.EndUpdate();
    }
    }

    /// <summary>
    /// Class to add a "Ascending" field to a ColumnHeader object so we can
    /// determine if the column was last in ascending or descending order.
    /// </summary>
    public class myColumnHeader : ColumnHeader
    {
    public bool Ascending;
    public myColumnHeader(string text, int width, HorizontalAlignment
    align, bool ascending)
    {
    this.Text = text;
    this.Width = width;
    this.TextAlign = align;
    this.Ascending = ascending;
    }
    }


    /// <summary>
    /// Class to wrap a listview item. Wrapper class adds sorting
    functionality.
    /// </summary>
    public class lviWrapper
    {
    internal ListViewItem m_Item;
    internal int m_iColumn;

    public lviWrapper (ListViewItem Item, int iColumn)
    {
    m_Item = Item;
    m_iColumn = iColumn;
    }

    public string Text
    {
    get
    {
    return m_Item.SubItems[m_iColumn].Text;
    }
    }

    public class lviComparer : IComparer
    {
    bool ascending;
    public lviComparer(bool ascending)
    {
    this.ascending = ascending;
    }

    public int Compare(object x, object y)
    {
    lviWrapper xlvi = (lviWrapper)x;
    lviWrapper ylvi = (lviWrapper)y;

    string xText = xlvi.m_Item.SubItems[xlvi.m_iColumn].Text;
    string yText = ylvi.m_Item.SubItems[ylvi.m_iColumn].Text;

    return xText.CompareTo(yText) * (this.ascending ? 1 : -1);
    }
    }
    }

  • 07-21-2008 4:38 In reply to

    Re: Sorting a ListView

    Thank you Michael Lipp.

Page 1 of 1 (2 items)