kvnsdr
asked on
C#: Listview column sorter, numerical Decending???
I found a very nice MicroSoft KB Article on ListView Column Sorting thats allows clicking column headers to sort accending or decending much like native Windows Operating system Listviews do. HOW TO: Sort a ListView Control by a Column in Visual C# .NET http://support.microsoft.com/?kbid=319401
However, one of my five listview columns has numbers and does not sort the way I would like. I realize when the listview is first loaded via an SQL table sort query.
Q. How can I enable a ListView to sort the text columns one way and the numreical columns another way???
Upon loading it looks like this:
150
110
100
90
8
5
2
1
After a column click: (Accending)
1
10
110
12
13
153
16
2
23
24
3
After a column click: (Decending)
3
23
24
185
17
However, one of my five listview columns has numbers and does not sort the way I would like. I realize when the listview is first loaded via an SQL table sort query.
Q. How can I enable a ListView to sort the text columns one way and the numreical columns another way???
Upon loading it looks like this:
150
110
100
90
8
5
2
1
After a column click: (Accending)
1
10
110
12
13
153
16
2
23
24
3
After a column click: (Decending)
3
23
24
185
17
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry, my mistake..
Another error though: Input string was not in a correct format:
MyCompare (Line 17):
return Convert.ToInt32(x).Compare To(Convert .ToInt32(y ));
ListViewColumnSorter (Line 58):
compareResult = ObjectCompare.Compare(list viewX.SubI tems[Colum nToSort].T ext,listvi ewY.SubIte ms[ColumnT oSort].Tex t);
Another error though: Input string was not in a correct format:
MyCompare (Line 17):
return Convert.ToInt32(x).Compare
ListViewColumnSorter (Line 58):
compareResult = ObjectCompare.Compare(list
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Works like a charm, thank you......... The completed code is below:
Put this class in your project:
public class MyCompare: System.Collections.ICompar er
{
private System.Collections.CaseIns ensitiveCo mparer ciComparer = new System.Collections.CaseIns ensitiveCo mparer();
private System.Text.RegularExpress ions.Regex regex = new System.Text.RegularExpress ions.Regex (@"^\d*$") ; // Regex(@"\d*");
public int Compare(object x, object y) // Assuming x, y are Strings (from ListView.Text)
{
if(regex.Match((string)x). Success&&r egex.Match ((string)y ).Success)
return Convert.ToInt32(x).Compare To(Convert .ToInt32(y ));
else
return ciComparer.Compare(x, y);
}
}
And then change the code:
private CaseInsensitiveComparer ObjectCompare;
to:
private MyCompare ObjectCompare;
And change:
ObjectCompare = new CaseInsensitiveComparer();
to:
ObjectCompare = new MyCompare();
Put this class in your project:
public class MyCompare: System.Collections.ICompar
{
private System.Collections.CaseIns
private System.Text.RegularExpress
public int Compare(object x, object y) // Assuming x, y are Strings (from ListView.Text)
{
if(regex.Match((string)x).
return Convert.ToInt32(x).Compare
else
return ciComparer.Compare(x, y);
}
}
And then change the code:
private CaseInsensitiveComparer ObjectCompare;
to:
private MyCompare ObjectCompare;
And change:
ObjectCompare = new CaseInsensitiveComparer();
to:
ObjectCompare = new MyCompare();
How would you use this code in the a project. I'm trying to sort a listview.
ASKER
I initially submitted this question regarding an article and code found at Microsoft. Go to the link below to start with, then back here to fill in a few blanks.......
http://support.microsoft.com/?kbid=319401
http://support.microsoft.com/?kbid=319401
ASKER
'MyCompare': member names cannot be the same as their enclosing type