Home Forums WinForms controls Xceed Grid for WinForms Sorting Groups by count of subitems

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

    Hello,

    I have data in the grid that is grouped, this groups itself could contain sub groups and so on..
    Up to now I calculate 2 values of sub items:
    – 1st all Data items: GetSortedDataRows(Recursive) – All Items
    – 2nd only direct children: ex. thisGroup.groups.count

    Now I want to sort the groups in one level by the count of direct children,
    but I couldn’t find any way to solve the problem.

    Is anybody out there, who can help me ?

    Thanks in Advance
    Robert

    Imported from legacy forums. Posted by robert (had 2353 views)

    User (Old forums)
    Member
    Post count: 23064

    It is possible to do so by setting your own DataComparer to all of your columns.

    Your dataComparer must find out if the object being compared is a group’s key or a cell’s value. This must be done through a reference to the column which the dataComparer resides on.

    First, check if the DataComparer’s column is currently being grouped by. If it is, return the grouping level. If the column is grouped, we now know that the values being compared are in fact group keys.

    Using that level, find the precise group for both object. Once you have a reference for both groups being compared, call GetSortedDataRowCount(true) on them to get the group’s dataRows count. Use these count to return the right result.

    If the dataComparer’s column is not being grouped by, implement basic comparison.

    Note that doing this kind of sorting will be slower and slower the more you have rows, groups, and grouping levels. I tested it on a P3 512 Mb RAM, with a grid containing 3 columns, 1000 dataRows, and two grouping levels containing about 700 groups each… It took about 2 seconds to sort by a column (if that column is grouped by).

    Here is my DataComparer class that I created:

    using System;
    using System.Collections;
    using Xceed.Grid;

    namespace WindowsApplication1
    {
    /// <summary>
    ///
    /// Comparer class that sorts groups by rows count
    ///
    /// </summary>

    public class myComparer : IComparer
    {
    public myComparer(Xceed.Grid.Column Column)
    {
    this.m_Column = Column;
    }

    private int FindGroupLevel()
    {
    foreach( Group group in this.m_Column.ParentGrid.GroupTemplates )
    {
    if( group.GroupBy == this.m_Column.FieldName )
    {
    return group.Level;
    }
    }

    return – 1;
    }

    private Xceed.Grid.Group FindGroup( object key, int GroupLevel, Xceed.Grid.Collections.ReadOnlyGroupList Groups )
    {
    for( int i = 0; i < Groups.Count; i ++ )
    {
    Group group = Groups[ i ];

    if( group.Level == GroupLevel )
    {
    if( group.Key.Equals( key ) )
    {
    return group;
    }
    }
    else
    {
    Xceed.Grid.Group foundGroup = this.FindGroup( key, GroupLevel, group.Groups );

    if( foundGroup != null )
    return foundGroup;
    }
    }
    return null;
    }

    #region IComparer Members

    public int Compare( object x, object y )
    {

    // find if the column is grouped, if it is get the group

    int GroupLevel = this.FindGroupLevel();

    if( GroupLevel > -1 )
    {
    Xceed.Grid.Collections.ReadOnlyGroupList groupList = this.m_Column.ParentGrid.Groups;

    Group groupX = this.FindGroup( x, GroupLevel, groupList );
    Group groupY = this.FindGroup( y, GroupLevel, groupList );

    int groupXRowsCount = groupX.GetSortedDataRowCount( true );
    int groupYRowsCount = groupY.GetSortedDataRowCount( true );

    if( groupXRowsCount > groupYRowsCount )
    return 1;

    if( groupXRowsCount < groupYRowsCount )
    return -1;
    }

    //default comparison
    IComparable xComparable = x as IComparable;

    if( xComparable != null )
    return xComparable.CompareTo( y );

    IComparable yComparable = y as IComparable;

    if( yComparable != null )
    return -yComparable.CompareTo( x );

    return 0;
    }

    #endregion

    private Xceed.Grid.Column m_Column;

    }
    }

    Imported from legacy forums. Posted by PL (had 3401 views)

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