Listview filter and sort

  • Thread starter Confused about Listview
  • Start date
C

Confused about Listview

I have read previous articles about sorting the data
within a listview by clicking on the columns, or
filtering the data by entering a few letters. What
methods tie into these features?

Thx

Confused
 
M

MichaelLipp[MS]

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, 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).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);
}
}
}


This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top