Home - Forums-.NET - FlyGrid.Net (Windows Forms) - Sorting problems

FlyGrid.Net (Windows Forms)

.NET Datagrid - Fast, highly customizable, industry standards .NET data grid control for WinForms

This forum related to following products: FlyGrid.Net

Sorting problems
Link Posted: 18-Apr-2006 07:45
Hi,

I have 2 sorting problems: http://www.kingvest.de/bugs/sorting/sorting.html

In the first part of the video I'm sorting a column with doubles and nulls ... however the sorting seems to be totally arbitrarily? Is that because of the null values in some cells?

The second problem I have is that I only want to sort the root nodes. When I click on a column header to sort the column I don't want the subnodes to be sorted, however as you can see when I click on the symbol node, the subnodes get sorted as well. How can I achieve it that this doesn't happen anymore?

Thanks,

Tom
Link Posted: 19-Apr-2006 07:57
In this case you can use custom sorting:
[c#]
    private void InitFlyGrid()
    {
      //...do something...
      // connect to ColumnSortOrderChanging event handler
      this.flyGrid.ColumnSortOrderChanging += new ColumnSortOrderChangingHandler(flyGrid_ColumnSortOrderChanging);
    }

    //custom comparer
    public class NodesComparer : IComparer
    {
      private SortOrder order;
      private Column col;
      public NodesComparer(Column col, SortOrder order)
      {
        this.col = col;
        this.order = order;
      }
      #region IComparer Members
      public int Compare(object x, object y)
      {
        Node xnode = x as Node;
        Node ynode = y as Node;
        int result = 0 ;
        object xobj = xnode.GetCellValue(col);
        object yobj = ynode.GetCellValue(col);
        IComparable xvalue = xobj as IComparable;
        IComparable yvalue = yobj as IComparable;
        if (xvalue != null && order == SortOrder.Ascending)
        {
          result = xvalue.CompareTo(yvalue);
        }
        else if (yvalue != null && order == SortOrder.Descending)
        {
          result = yvalue.CompareTo(xvalue);
        }
        return result;
      }
      #endregion
    }
    
    //ColumnSortOrderChanging event handler
    private void flyGrid_ColumnSortOrderChanging(object sender, Column column, SortOrder value, ref bool sorted)
    {
      if (column is HierachyColumn)
      {
        FlyGrid fg = sender as FlyGrid;
        fg.Rows.Items.Sort(new NodesComparer(column, value));
        fg.Rows.RefreshNodes();
        sorted = true;
      }
    }
Link Posted: 19-Apr-2006 11:53
This doesn't work. Subnodes still get sorted.
Link Posted: 19-Apr-2006 13:50
Try this sample
Nodes sorted only in top level.
Link Posted: 19-Apr-2006 20:41
??? Tried your sample and subnodes get sorted as well? First, subnode node0 is on top and then subnode node9 is on top
Link Posted: 20-Apr-2006 07:22
Sorry for incorrectness, try this sample with the latest v1.4.0.4
Link Posted: 20-Apr-2006 07:30
now it works correct, thx
Link Posted: 20-Apr-2006 10:31
btw you didn't get back to me about the first part of the question:
\"In the first part of the video I'm sorting a column with doubles and nulls ... however the sorting seems to be totally arbitrarily? Is that because of the null values in some cells?\"

Can you please reply to that?

Thanks,

Tom
Link Posted: 20-Apr-2006 12:03
Please check how you are store data in cells.
Probably sorting procedure handles strings instead of doubles/singles.
I've tried to add nodes with Double values:
[c#]
Random rnd = new Random();
for(int i=0; i < count; i++)
{  
   flyGrid.Rows.Items.Add(new TreeViewNode(new object[]{rnd.NextDouble()}));
}


and sorting works fine.
Yes, there is a problem with nulls that already corrected.
Now you can replace your customer comparer code with following to correctly sort  nulls:

public int Compare(object x, object y)
{
  Node xnode = x as Node;
  Node ynode = y as Node;
  int result = 0 ;
  object xobj = xnode.GetCellValue(col);
  object yobj = ynode.GetCellValue(col);
  IComparable xvalue = xobj as IComparable;
  IComparable yvalue = yobj as IComparable;
  if (order == SortOrder.Ascending)
  {
    result = xvalue == null ? yvalue == null ? 0 : -1 : xvalue.CompareTo(yvalue);
  }
  else if (order == SortOrder.Descending)
  {
    result = yvalue == null ? xvalue == null ? 0 : -1 : yvalue.CompareTo(xvalue);
  }
  return result;
}

NB: this code correctly sorts doubles also.
Link Posted: 20-Apr-2006 12:22
thx, now it works correct