Can't find what you're looking for?

Contact us or post in our forums and we'll help you find an answer.

Filter Out Columns in FlexGrid for WinForms

Background:

Currently in WinForms FlexGrid, it is possible to only filter out rows using the FilterEditorForm, which opens upon clicking the Filter icon available in the column headers. This article shows you how to add an option in the FilterEditorForm in order to filter out columns in the grid as well.

Steps to Complete:

  1. Currently in WinForms FlexGrid, it is possible to only filter out rows using the FilterEditorForm, which opens upon clicking the Filter icon available in the column headers. This article shows you how to add an option in the FilterEditorForm in order to filter out columns in the grid as well.

    private void Flex_MouseClick(object sender, MouseEventArgs e)
            {
                
                    if (flex.HitTest(e.Location).Type == HitTestTypeEnum.FilterIcon)
                    {
                        foreach (Form frm in Application.OpenForms)
                        {
                            if (frm.Name == "FilterEditorForm" && frm.GetType().ToString() == "C1.Win.C1FlexGrid.FilterEditorForm")
                            {
                            customStrip = new ToolStrip();
                            customStrip.Dock = DockStyle.Bottom;
    
                            stripSplitButton = new ToolStripSplitButton();
                            stripSplitButton.Text = "Column List";
    
                            SetupColumnFilter();
                            stripSplitButton.DropDown.Closing += DropDown_Closing;
                            customStrip.Items.Add(stripSplitButton);
                            frm.Controls.Add(customStrip);
                                
                            }
                        }
                    
                }
       }
  2. Populate the SplitButton’s dropdown with column names as MenuItems.

    private void SetupColumnFilter() { 
                foreach(Column col in flex.Cols){
                    ToolStripMenuItem menuItem = new ToolStripMenuItem();
                    menuItem.Text = col.Caption == String.Empty?"Col "+col.Index:col.Caption;
                    menuItem.CheckOnClick = true;
                    menuItem.CheckedChanged += MenuItem_CheckedChanged;
                    menuItem.Checked = col.Visible;
                    menuItem.Tag = col;
                    stripSplitButton.DropDownItems.Add(menuItem);
                }
            }
  3. Handle MenuItem’s CheckedChanged event by filtering out the columns based on the checked state.

    private void MenuItem_CheckedChanged(object sender, EventArgs e)
            {
                ToolStripMenuItem item = sender as ToolStripMenuItem;
                if (item is null) { return; }
                Column col = item.Tag as Column;
                if (col is null) { return; }
                col.Visible = item.Checked;
            }
  4. Handle SplitButton’s DropDown’s Closing event to cancel when the CloseReason is ItemClicked. This prevents closing the dropdown upon selecting an item and helps in filtering out more than one column.

private void DropDown_Closing(object sender, ToolStripDropDownClosingEventArgs e)
        {
            if ( e.CloseReason == ToolStripDropDownCloseReason.ItemClicked)
                e.Cancel = true;
        }
Get the Latest Version of C1
Download the Free Trial Now