Using the C1FlexGrid Control > C1FlexGrid Filtering > Customizing filter behavior |
When filters are applied, the grid hides rows by setting their Visible property to False. But the grid also fires BeforeFilter and AfterFilter events that allow you to customize the filtering behavior. For example, assume that instead of showing and hiding rows you wanted to apply different styles to indicate whether the the rows pass the filter or not. This can be accomplished easily using this code:
To write code in C#
C# |
Copy Code
|
---|---|
public Form1() { InitializeComponent(); // configure grid _flex.AllowFiltering = true; _flex.DataSource = dtInvoices; // create style for rows excluded by the filter var cs = _flexStyles.Styles.Add("filteredOut"); cs.BackColor = Color.LightSalmon; cs.ForeColor = Color.DarkOrange; // connect handlers for the before and after filter events _flex.BeforeFilter += _flex_BeforeFilter; _flex.AfterFilter += _flex_AfterFilter; } |
The code creates a custom style that will be used to show rows that did not pass the filter (instead of making them invisible). Next, the code attaches handlers for the BeforeFilter and AfterFilter events. The event handlers are listed below:
To write code in C#
C# |
Copy Code
|
---|---|
// suspend painting before filter is applied void _flex_BeforeFilter(object sender, CancelEventArgs e) { _flexStyles.BeginUpdate(); } // apply styles after the filter has been applied void _flexStyles_AfterFilter(object sender, EventArgs e) { // get style used to show filtered out rows var cs = _flex.Styles["filteredOut"]; // apply style to all rows for (int r = _flexStyles.Rows.Fixed; r < _flexStyles.Rows.Count; r++) { var row = _flexStyles.Rows[r]; if (row.Visible) { // normal row, reset style row.Style = null; } else { // filtered row, make visible and apply style row.Visible = true; row.Style = cs; } } // resume updates _flexStyles.EndUpdate(); } |
The BeforeFilter event handler calls the new BeginUpdate method to prevent the grid from repainting itself until we are done applying the custom style to the rows that are filtered out. The BeginUpdate and EndUpdate methods replace the Redraw property which has been deprecated.
The AfterFilter event handler starts by retrieving the style we created to show the rows that have been filtered out. It then scans the rows on the grid and applies the new style to all rows that have the Visible property set to False. These are the rows that were hidden by the filter. Once this is done, the code calls EndUpdate to restore grid updating.