Home Forums WinForms controls Xceed Grid for WinForms Sorting Question

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #13307 |

    I have a field in my database that is numeric but is stored as alpha. I would like to sort it as numeric.

    Is there a way in the grid to display one set of data but sort by another? Or another suggestion on how to sort the data?

    I was hoping to do it using the Sorted events but dont see a way to handle multipul sorted columns, short of doing all the sorting in code.

    Thanks in advance.
    Joby

    Imported from legacy forums. Posted by Joby (had 1919 views)

    User (Old forums)
    Member
    Post count: 23064

    You can create a custom comparer, that compares the numeric values of the strings, and assign it to the <b>DataComparer</b> property of the column. Such a custom comparer is just a class that implements the <b>IComparer</b> interface and has a method <b>Compare</b>. Like this:
    <code>public class myComparer : System.Collections.IComparer
    {
    int IComparer.Compare(object x, object y)
    {
    try
    {
    int ix = Convert.ToInt32(x);
    int iy = Convert.ToInt32(y);

    if (ix < iy) then return -1;
    else if (ix > iy) then return 1;
    else return 0;
    }
    catch(Exception ex)
    {
    throw new ArgumentException(“Can’t compare arguments”);
    }
    }
    }</code>
    Then you can assign your comparer to the column:
    <code>myGrid.Columns[“myColumn”].DataComparer = new myComparer();</code>

    Imported from legacy forums. Posted by Tommy (had 3064 views)

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.