Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C#: Listview column sorter, numerical Decending???

Posted on 2004-09-09
7
Medium Priority
?
2,072 Views
Last Modified: 2008-01-09
  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

0
Comment
Question by:kvnsdr
  • 4
  • 2
7 Comments
 
LVL 10

Accepted Solution

by:
eternal_21 earned 500 total points
ID: 12019660
Put this class in your project:

  public class MyCompare: System.Collections.IComparer
  {
    private System.Collections.CaseInsensitiveComparer ciComparer = new System.Collections.CaseInsensitiveComparer();
    private System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\d*");

    public int Compare(object x, object y) // Assuming x, y are Strings (from ListView.Text)
    {
      if(regex.Match((string)x).Success&&regex.Match((string)y).Success)
        return Convert.ToInt32(x).CompareTo(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();
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 12020458
So far so good, except I get the following error on the new class:

 'MyCompare': member names cannot be the same as their enclosing type
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 12020676
Sorry, my mistake..

Another error though: Input string was not in a correct format:

MyCompare (Line 17):
return Convert.ToInt32(x).CompareTo(Convert.ToInt32(y));

ListViewColumnSorter (Line 58):
compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text,listviewY.SubItems[ColumnToSort].Text);
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Assisted Solution

by:eternal_21
eternal_21 earned 500 total points
ID: 12020846
Sorry, kvnsdr

Change Regex(@"\d*"), to Regex(@"^\d*$")... I made a little mistake there!
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 12029834
Works like a charm, thank you.........  The completed code is below:


Put this class in your project:

  public class MyCompare: System.Collections.IComparer
  {
    private System.Collections.CaseInsensitiveComparer ciComparer = new System.Collections.CaseInsensitiveComparer();
    private System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.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&&regex.Match((string)y).Success)
        return Convert.ToInt32(x).CompareTo(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();
0
 

Expert Comment

by:cjsteynberg
ID: 12378531
How would you use this code in the a project.  I'm trying to sort a listview.
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 12380689
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question