Xceed Grid for WinForms v4.3 Documentation
Sorting and filtering
Welcome to Xceed Grid for WinForms v4.3 > Basic Concepts > Sorting and filtering

Sorting

Data rows can be sorted in either a descending or ascending direction according to the values of one or more columns. Groups are automatically sorted in the same direction as the column set in the group's GroupBy property. In order to sort groups that were created using only custom grouping, the group's GroupBy property must be set even though it will not cause groups to be created based on the values of the column set in its GroupBy property. 

To sort a column, its SortDirection property must be set. Setting a column's SortDirection property to either ascending or descending, will automatically add it to the grid's collection of SortedColumns. Setting the column's SortDirection property to none will remove any sorting presently applied on the column and remove it from the grid's collection of SortedColumns. 

To do custom sorting, set the DataComparer property of the column whose values are to be sorted. Any class that implements the IComparer interface can be used.

Filtering

You can filter the items displayed in a grid by casting the DataSource as a BindingSource and setting its Filter property.

Currently, there is no built-in functionality that allows for data rows to be filtered automatically, however there are three possible workarounds that can simulate filtering: 

1- Set the Visible property of the rows that need to be filtered to false (this is is normally done in the CellValueChanged event of the datarow template). This workaround would be sufficient in the case of grids that do not contain much data, however in the case of grids that contain a lot of data, the process can be slow. 

In the following example, all the rows whose products are discontinued will be filtered by having their Visible properties set to false. However if there are groups in the grid, the empty groups (those whose data rows are not visible), will still be visible.

View code

C# Copy Code

using Xceed.Grid; 

GridControl grid = new GridControl();
grid.Dock = DockStyle.Fill; 

this.Controls.Add( grid ); 

grid.DataRowTemplate.CellValueChanged += new EventHandler( this.dataRowTemplate_CellValueChanged ); 

grid.DataSource = dataSet11;
grid.DataMember = dataSet11.Tables[ 0 ]; 

private void dataRowTemplate_CellValueChanged(object sender, System.EventArgs e)
{
   try
   {
      Xceed.Grid.DataRow row = ( Xceed.Grid.DataRow )sender;
      row.Visible = !( bool )row.Cells[ "Discontinued" ].Value;      
   }
   catch( Exception exception )
   {
      MessageBox.Show( exception.ToString() );
   }
}

2- Create a group that will contain the filtered rows by handling the QueryGroupKeys and GroupAdded events. For example, if you want to filter but do not want the data rows in your grid to appear grouped, the following code can be used:

The code below will create 2 groups which will provide the same presentation as if the data rows were not grouped: Neither of the groups will have rows in their header or footer sections, the "Filtered" group will be collapsed and the visible group will have no margin.

View code

C# Copy Code

using Xceed.Grid; 

GridControl grid = new GridControl();
grid.Dock = DockStyle.Fill; 

this.Controls.Add( grid ); 

Group groupTemplate = new Group();
groupTemplate.Margin.Visible = false; 

grid.GroupTemplates.Add( groupTemplate ); 

grid.QueryGroupKeys += new QueryGroupKeysEventHandler( this.grid_QueryGroupKeys );
grid.GroupAdded += new GroupAddedEventHandler( this.grid_GroupAdded ); 

grid.DataSource = dataSet11;
grid.DataMember = dataSet11.Tables[ 0 ]; 

private void grid_QueryGroupKeys( object sender, QueryGroupKeysEventArgs e )
{
   try
   {
      if( ( bool )e.DataRow.Cells[ "Discontinued" ].Value )
      {
         e.GroupKeys[ 0 ] = "Filtered";
      }
      else
      {
         e.GroupKeys[ 0 ] = "Visible";
      }
   }
   catch( Exception exception )
   {
      MessageBox.Show( exception.ToString() );
   }

private void grid_GroupAdded(object sender, GroupAddedEventArgs e)
{
   try
   {
      if( e.NewGroup.Key.Equals( "Filtered" ) )       
          e.NewGroup.Collapse();         
   }
   catch( Exception exception )
   {
      MessageBox.Show( exception.ToString() );
   }
}

If you have and want groups in your grid however you also want to filter some rows, the following code could be used:

The code below will create 3 groups: the first will contain all products whose names begin with the letter A to N, the second group will contain all products whose names begin with the letter M to Z while all the groups that are discontinued will be in a third group called "Filtered" which will not be visible.

If the grid contains one or more GroupByRow's, the empty groups will appear in it. You can change the title of the "invisible" group as it appears in the GroupByRow, by setting the group's GroupByRowTitle property.

View code

C# Copy Code

using Xceed.Grid; 

GridControl grid = new GridControl();
grid.Dock = DockStyle.Fill; 

this.Controls.Add( grid ); 

Group groupTemplate = new Group();
groupTemplate.HeaderRows.Add( new GroupManagerRow() ); 

grid.GroupTemplates.Add( groupTemplate ); 

grid.QueryGroupKeys += new QueryGroupKeysEventHandler( this.grid_QueryGroupKeys );
grid.GroupAdded += new GroupAddedEventHandler( this.grid_GroupAdded ); 

private void grid_QueryGroupKeys( object sender, QueryGroupKeysEventArgs e )
{
   try
   {
      string value = ( ( string )e.DataRow.Cells[ "ProductName" ].Value ).Substring( 0, 1 ).ToUpper();    

       if( ( bool )e.DataRow.Cells[ "Discontinued" ].Value )
      {
         e.GroupKeys[ 0 ] = "Filtered";
      }
      else if( value.CompareTo( "M" ) > 0 )
      {
         e.GroupKeys[ 0 ] = "N-Z";     
      }
      else
      {
         e.GroupKeys[ 0 ] = "A-M";
      }
   }
   catch( Exception exception )
   {
      MessageBox.Show( exception.ToString() );
   }

private void grid_GroupAdded(object sender, GroupAddedEventArgs e)
{
   try
   {
      if( e.Group.Key.Equals( "Filtered" ) )       
      {
          e.Group.HeaderRows.Clear();
          e.Group.Collapse();         
      }
   }
   catch( Exception exception )
   {
      MessageBox.Show( exception.ToString() );
   }
}

3- Bind the grid to a DataView object! By setting the RowFilter property of the DataView object, the grid will only display the rows which have not been filtered out by the DataView object.

Things you should consider