How to: Display a Custom String in the Filter Panel

The default filter handling in the XtraGrid shows and hides the filter panel when a filter is activated and deactivated. If the filter panel is shown all the time, it displays an empty string when there is no filter configured. Using an event handler, it is possible to customize the filter string that is displayed, both for active filters and for the situation where the filter is blank.

  1. Starting from a new WinForms application, I drop an XtraGrid control on the form and dock it to its parent.
  2. For the GridView, I set the property OptionsView.ShowFilterPanelMode to ShowAlways. It is possible to customize the filter string without this setting, but in order to demonstrate the various scenarios, I want the panel to be always visible.
  3. For the purpose of this example, I’m going to use a simple list as my data source. In Form1.cs, I add the following small class to store my test data:

public class Data {

public Data(string text) {

this.text = text;

}

private string text;

public string Text {

get { return text; }

set { text = value; }

}

}

  1. In the constructor of Form1, I add this code to create some test data and bind it to the grid:

ListData> list = new ListData>();

list.AddRange(new Data[] {

new Data("An entry"),

new Data("A second entry"),

new Data("Yet another entry")

});

gridControl1.DataSource = list;

  1. I run the application. I can configure a filter for the column “Text” and the view is filtered accordingly. When I switch back to “All” records in the filter popup, the filter string is removed from the filter panel and it is now empty.

  1. Back in Visual Studio, I create a handler for the event CustomFilterDisplayText and I add the following code. On every call into the event, the property value of the event args is either null, if there’s no filter currently configured, or it contains a CriteriaOperator object that represents the filter which has been configured. It is important to set the Handled property in the event args to true, so that further internal handling doesn’t override what my handler has done.

if (e.Value == null)

e.Value = "No Filter Set";

else

e.Value = "Filter Set: " + e.Value.ToString( );

e.Handled = true;

  1. I run the application again, and now I can see how my custom filter texts are displayed when I switch filtering on and off.

  1. Currently I’m just using ToString() to evaluate the filter criteria if necessary. Of course it is also possible to dig into the criteria object in e.Value and create a more complex filter string. This small method demonstrates a slightly more elaborate algorithm – but note that it only covers one of several possible scenarios.

private string GetFilterString(CriteriaOperator op) {

BinaryOperator binaryOp = op as BinaryOperator;

if (!Object.ReferenceEquals(binaryOp, null))

return String.Format("Checking whether {0} is {1} to {2}",

binaryOp.LeftOperand, binaryOp.OperatorType, binaryOp.RightOperand);

else

return op.ToString( );

}

  1. To use this method, I need to modify the event handler to call it in the else branch:

e.Value = GetFilterString(e.Value as CriteriaOperator);

  1. When I run my application a final time, the filter outputs the custom filter string when a filter is configured.

Dynamic behavior through events, that’s a central theme in the XtraGrid.

Thanks for watching, and of course for chosing DevExpress!