Xceed Grid for WinForms v4.3 Documentation
Welcome to Xceed Grid for WinForms v4.3 / Task-Based Help / Grouping and sorting / Combining automatic and custom grouping
In This Topic
    Combining automatic and custom grouping
    In This Topic

    The GroupBy property and the QueryGroupKeys event can be used together to provide custom grouping criteria. The GroupBy property can be set using either code or the property grid, however custom grouping must be done via code.

    Demonstration

    C# Copy Code

    Group group = new Group();
    group.GroupBy = "ShipCountry";

    In order for a group to have an effective visual representation in  the grid,  it must contain at least one row in either its header or footer rows.

    C# Copy Code

    group.HeaderRows.Add( new GroupManagerRow() );

    gridControl1.GroupTemplates.Add( group ); 

    //Subscribe to the QueryGroupKeys event.
    gridControl1.QueryGroupKeys += new QueryGroupKeysEventHandler( this.grid_QueryGroupKeys );

    It is then necessary to handle the QueryGroupKeys event to determine when a new group is created. For example, the code in the event handler below will group all data rows that have the same value for ShipCountry but in an case insensitive manner. This means that both "Canada" and "canada" will be grouped together.

    C# Copy Code
    private void grid_QueryGroupKeys( object sender, QueryGroupKeysEventArgs e )
    {
       try
       {
          for( int i = 0; i < e.GroupKeys.Count; i++ )
          {
             if( e.GroupKeys[ i ] is string )
             {
                 e.GroupKeys[ i ] = ( ( string )e.GroupKeys[ i ] ).ToUpper();
             }
          }
       }
       catch( Exception exception )
       {
          MessageBox.Show( exception.ToString() );
       }
    }