Xceed Grid for WinForms v4.3 Documentation
Welcome to Xceed Grid for WinForms v4.3 / Basic Concepts / Events / Using the QueryGroupKeys event

    Using the QueryGroupKeys event

    The QueryGroupKeys event is raised after a data row has been created in order to group it. It allows custom grouping of the grid's data rows.

    Basic steps - C#

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

    • Obtain a reference to a GridControl object. 

    • Add the desired number of groups to the grid's GroupTemplates property. 

    • Subscribe to the QueryGroupKeys event of the GridControl object using the generic EventHandler delegate class 

    • Create a new method that will handle the events that are raised. 

    • Place the desired code in the newly created event handler.

    Basic steps - VB.NET

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

    • Obtain a reference to a GridControl object. 

    • Add the desired number of groups to the grid's GroupTemplates property. 

    • Subscribe to the QueryGroupKeys event of the GridControl object using the AddHandler/AddressOf keywords 

    • Create a new method that will handle the events that are raised. 

    • Place the desired code in the newly created event handler.

    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() )
    gridControl1.GroupTemplates.Add( New Group() )
     
    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 )
     
      Try
        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
      Catch exception As Exception
        MessageBox.Show( exception.ToString() )
      End Try
    End Sub
     
    ' If you no longer wish to handle the QueryGroupKeys events that are raised,
    ' you can unsubscribe from the event notification by doing the following:
    RemoveHandler gridControl1.QueryGroupKeys, AddressOf Me.grid_QueryGroupKeys
    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 );