How to Edit FlexGrid Filters at Runtime in a WinForms DataGrid
Tags: webdev, devops, dotnet, tutorial
Published: 2024-02-02 15:36:29

---
canonical_url: https://developer.mescius.com/blogs/how-to-edit-flexgrid-filters-at-runtime-in-a-winforms-datagrid
description: Learn how to edit FlexGrid filters at runtime in a WinForms datagrid application. See more from ComponentOne today.
---
**What You Will Need**
- [ComponentOne WinForms Edition](https://developer.mescius.com/componentone/winforms-ui-controls)

- Visual Studio 2022 with .NET 8

**Controls Referenced**
- [FlexGrid](https://www.nuget.org/packages/C1.Win.FlexGrid)
- [FilterEditor](https://www.nuget.org/packages/C1.Win.DataFilter) (part of DataFilter)
- [Documentation](https://developer.mescius.com/componentone/docs/win/online-datafilter/filtereditor.html)
- [GitHub Sample](https://github.com/GrapeCity/ComponentOne-WinForms-Samples/tree/master/Core/DataFilter/CS/DataFilterExplorer) - DataFilterExplorer

**Tutorial Concept**
The article demonstrates how to combine two of our controls to provide advanced filtering, and summaries can be displayed in the footer of the FlexGrid after filtering data.

---

While FlexGrid is jam-packed with cool features that can be implemented directly in the designer without writing a single line of code, one of our latest samples available on GitHub showcases the integration of our FilterEditor with FlexGrid for WinForms. This sample showcases how FilterEditor can be invoked from the context menu at runtime and how filter summaries can be displayed in the footer of the grid after filtering data, as shown in the screenshot below:

![WinForms Datagrid Filtering](//cdn.mescius.io/umb/media/tn5hqaxa/01-flexgrid.png?rmode=max&width=754&height=536)

This blog aims to dive through the code and demonstrate how the above sample was laid out. Without further ado, let’s get started!

## Quick Setup

To begin, we have to drag and drop both a FlexGrid and a FilterEditor to the form. To bind the FlexGrid and FilterEditor together, simply set the DataSource property for both controls to the same object.

```
    c1FlexGrid1.DataSource = dt;
    c1FilterEditor1.DataSource = dt;
```

This will provide the same values for both the FlexGrid and FilterEditor controls. To link them together and apply the changes presented in the filter to the grid, we need to use the FilterEditor’s ApplyFilterAsync() method. We’ll also need some event to call the ApplyFilterAsync() method after we are done editing our filter values.

## Edit Filter with Form Button

Typically, a button makes a perfect control to accomplish firing an event that we want to be applied to the grid, so we create two buttons: the first for applying the filter and the second for resetting the filter.

![WinForms Datagrid Filtering](//cdn.mescius.io/umb/media/cpsa4wtn/02-flexgrid.png?rmode=max&width=753&height=309)

Below is a screenshot of the simple lines of code included in these button-click events that make the magic happen:

![WinForms Datagrid Filtering](//cdn.mescius.io/umb/media/odcflfmp/03-flexgrid.png?rmode=max&width=727&height=226)

This is enough to add your basic filtering functionality to the FlexGrid via the FilterEditor control. The complete GitHub sample has some additional features we want to highlight, and we recommend viewing the code directly.

## Edit Filter with Context Menu

To add a customized context menu to your FlexGrid, you’ll want to add the C1.Win.Command.dll to your project via NuGet. Inside the Form_Load event, you’ll need to create a C1CommandHolder object to hold the context menu commands you’ll want to add to your FlexGrid. This can be done with the following code:

```
    C1CommandHolder ch = C1CommandHolder.CreateCommandHolder(this);                   
```

For this example, we’ll create a copy command, and through the Click event, we’ll be able to copy the command when the menu item is clicked. Additionally, we’ll use a query handler so that the commands are kept up to date automatically through C1Command as follows:

```
    // Create and set up the Copy command
    C1Command cmdCopy = ch.CreateCommand();
    cmdCopy.Text = "&Copy";
    cmdCopy.Click += new C1.Win.C1Command.ClickEventHandler(clickCopy);
    cmdCopy.CommandStateQuery += new C1.Win.C1Command.CommandStateQueryEventHandler(queryCopy);              
```
       
Next, we’ll need to create a context menu to hold the copy command and assign that context menu to the FlexGrid control. The context menu will be created when you use C1CommandHolder to create a command and assign it to the context menu. This is done through the C1CommandHolder.CreateCommand() and C1CommandHolder.SetC1ContextMenu() methods:

```
    C1ContextMenu cm = ch.CreateCommand(typeof(C1ContextMenu)) as C1ContextMenu;

    // Fill it with the links to the commands
    cm.CommandLinks.Add(new C1CommandLink(cmdCopy));
    ch.SetC1ContextMenu(flexGrid1, cm);                           
```

Lastly, you’ll want to invoke the clickCopy() method to handle the copy command action. The queryCopy() method will provide the current state of the copy command to ensure the proper value is being copied. When you click the copy command from the context menu, the value inside the FlexGrid cell will be copied to your clipboard:

```
    private void clickCopy(object sender, C1.Win.Command.ClickEventArgs e)
        {
            this.flexGrid1.Copy();
        }
    //provides the current state of the copy command
    private void queryCopy(object sender, C1.Win.Command.CommandStateQueryEventArgs e)
        {
            e.Enabled = this.flexGrid1.Selection.IsValid;
        }
```

Here, we can see the Copy command in our context menu, with paste functionality working through the standard keyboard shortcut of Ctrl+V:

![WinForms Datagrid Filtering](//cdn.mescius.io/umb/media/uexbi402/04-flexgrid.png?rmode=max&width=755&height=494)

## Filter Summary Footer, Checkbox Filtering, and More

In our sample provided [<span>on GitHub</span>](https://github.com/GrapeCity/ComponentOne-WinForms-Samples/tree/master/Core/DataFilter/CS/DataFilterExplorer "https://github.com/GrapeCity/ComponentOne-WinForms-Samples/tree/master/Core/DataFilter/CS/DataFilterExplorer"), features such as a checkbox that can be toggled to apply or remove the filter from the grid, as well as a filter summary set into the footer of the grid, can be seen:

![WinForms Datagrid Filtering](//cdn.mescius.io/umb/media/tvzauhj5/05-flexgrid.png)

In our sample, a user control is created that handles the checkbox filtering and summary panel. To see the exact code, feel free to check out the [<span>exact file here containing the checkbox filtering and filter summary footer</span> ](https://github.com/GrapeCity/ComponentOne-WinForms-Samples/blob/master/Core/DataFilter/CS/DataFilterExplorer/Samples/Controls/FilterEditorSummaryPanel.cs "https://github.com/GrapeCity/ComponentOne-WinForms-Samples/blob/master/Core/DataFilter/CS/DataFilterExplorer/Samples/Controls/FilterEditorSummaryPanel.cs")or download the complete sample from the main GitHub page linked above. To learn more about ComponentOne controls and everything Mescius has to offer, check out [our main page](https://developer.mescius.com/componentone "https://developer.mescius.com/componentone") for more information!