Xceed Grid for WinForms v4.3 Documentation
Custom grouping

Welcome to Xceed Grid for WinForms v4.3 > Advanced Concepts > Custom grouping

Custom grouping is done via code. First, the desired groups must be created and added to the GroupTemplates property of the grid. Next, the grid's QueryGroupKeys event needs to be handled and the code necessary to group the data rows added. 

Keep in mind that in order for the groups to be sorted, their GroupBy property must be set. 

It is also possible to derive from the Group class and override the CalculateKey method to provide custom grouping.

Basic steps - C#

To subscribe to the QueryGroupKeys event, the following steps must be performed:

Basic steps - VB.NET

To subscribe to the QueryGroupKeys event, the following steps must be performed:

Demonstration

The following example will group all the data rows according to the value of the ShipCountry cell. All data rows whose ShipCountry cell's value begins with A to M will be grouped together while those from N to Z will be grouped together.

The second group level will regroup all data rows according to the value of the ShipCity cell. All data rows whose ShipCity cell's value begin with the 2 same letters will be grouped together. For example, Mannheim and Marseille will be regrouped together.

VB.NET
Copy Code
Imports Xceed.Grid

gridControl1.GroupTemplates.Add( New Group( "ShipCountry" ) )

gridControl1.GroupTemplates.Add( New Group( "ShipCity" ) )

AddHandler gridControl1.QueryGroupKeys, AddressOf Me.grid_QueryGroupKeys

' This method will handle the QueryGroupKeys events that are raised.

Private Sub grid_QueryGroupKeys( ByVal sender As Object, _

                                 ByVal e As QueryGroupKeysEventArgs ) Handles gridControl1.QueryGroupKeys

  Dim countryValue As String = e.DataRow.Cells( "ShipCountry" ).Value.ToString().Substring( 0, _

                                                                                1 ).ToUpper()

  If( countryValue.CompareTo( "M" ) > 0 ) Then

    e.GroupKeys( 0 ) = "N-Z"

  Else

    e.GroupKeys( 0 ) = "A-M"

  End If

  Dim cityValue As String = e.DataRow.Cells( "ShipCity" ).Value.ToString().Substring( 0, _

                                                                                      2 ).ToUpper()

  e.GroupKeys( 1 ) = cityValue

End Sub
C#
Copy Code
using Xceed.Grid; 

gridControl1.GroupTemplates.Add( new Group( "ShipCountry" ) ); 

gridControl1.GroupTemplates.Add( new Group( "ShipCity" ) ); 

gridControl1.QueryGroupKeys += new QueryGroupKeysEventHandler( this.grid_QueryGroupKeys ); 

// This method will handle the QueryGroupKeys events that are raised.

private void grid_QueryGroupKeys( object sender, QueryGroupKeysEventArgs e ) 

{    

  try    

  {       

    string countryValue = e.DataRow.Cells[ "ShipCountry" ].Value.ToString().Substring( 0, 1 ).ToUpper();       

    if( countryValue.CompareTo( "M" ) > 0 )       

    {          

      e.GroupKeys[ 0 ] = "N-Z";       

    }       

    else       

    {          

      e.GroupKeys[ 0 ] = "A-M";             

    }       

    string cityValue = e.DataRow.Cells[ "ShipCity" ].Value.ToString().Substring( 0, 2 ).ToUpper();             

    e.GroupKeys[ 1 ] = cityValue;    

  }   

  catch( Exception exception )    

  {        

    MessageBox.Show( exception.ToString() );    

  } 

} 

//If you no longer wish to handle the QueryGroupKeys events that are raised, 

//you can unsubscribe from the event notification by doing the following:  

gridControl1.QueryGroupKeys -= new QueryGroupKeysEventHandler( this.grid_QueryGroupKeys );