ComponentOne FlexGrid for WinForms, as the name states, provides an easy-to-use, flexible grid control for creating user-friendly interfaces that display, edit, format, organize, summarize, and print tabular data. C1FlexGrid provides all the basic plus advanced features such as sorting, filtering, cell merging, etc. C1FlexGrid has built-in column filtering feature that is enabled using the AllowFiltering property. Once filtering is enabled, the filters can be edited for each column by clicking the filter icon on the column headers and a Filter dropdown is displayed. Through this dropdown, the values on which data is to be filtered, are selected. In this blog, we will be discussing how to customize the FilterDropDown so it may best meet your needs. The particular scenerio is to give the users ability to sort and filter the grid using a single dropdown menu. avi_C1FlexGrid_CustomFilter

Implementation

Basically, the idea is to get the Filter dropdown menu as a separate Form and then add buttons in the ToolStrip of that Form to perform any desired operation (for example, sorting). The approach includes the following steps:

Step1:

The first step is to check if the Filter icon in the Column header has been clicked. In order to fulfill the purpose, the HitTest.html "HitTest") method of C1FlexGrid can be used in the MouseClick event. Now, customize the dropdown Form in case it is. In addition to this, save the index of the Column in a variable so that it can be used to further perform sorting on that particular column.


private void C1FlexGrid1_MouseClick(object sender, MouseEventArgs e)  
{  
if (C1FlexGrid1.HitTest(e.Location).Type == C1.Win.C1FlexGrid.HitTestTypeEnum.FilterIcon)  
{  
CustomFilterMenu();  
HitTestInfo hti = C1FlexGrid1.HitTest(e.X, e.Y);  
col = hti.Column;  
}  
}  

Here 'CustomFilterMenu' is the method which includes the complete definition for the customization of the Filter dropdown menu.

Step2:

Since we have checked if the Filter icon has been clicked, now it is time to customize the Filter dropdown menu of the grid and add buttons in the ToolStrip of the menu. Filter dropdown menu is not directly accessible as an object in C1FlexGrid but it is considered as a separate Form by the Application when it is in Open state. Therefore, the Filter dropdown menu can be used as a Form to implement this functionality. Check the Open Forms in the Application using the 'OpenForms' of the Application class. In the OpenForms collection, traverse every item and check if the name and type of the Form is 'FilterEditorForm' and 'C1.Win.C1FlexGrid.FilterEditorForm' respectively.


private void CustomFilterMenu()  
{  
foreach (Form frm in Application.OpenForms)  
{  
if (frm.Name == "FilterEditorForm" & frm.GetType().ToString() == "C1.Win.C1FlexGrid.FilterEditorForm")  
{  
//Perform the desired functionality  
}  
}  
}  

Step3:

After getting the FilterEditorForm, one can modify it according to their own requirements. As we are implementing sorting, we need to add two buttons in the Toolstrip of the Form. Using the Click event of those two newly added buttons, sorting can be performed in the grid.


if (frm.Name == "FilterEditorForm" & frm.GetType().ToString() == "C1.Win.C1FlexGrid.FilterEditorForm")  
{  
filterForm = frm;  
filterForm.Width = filterForm.Width + 70;  
frm.Refresh();  

foreach (object item in filterForm.Controls)  
{  
dynamic ts = item as ToolStrip;  
if (ts != null)  
{  
ToolStripButton tsASC = new ToolStripButton();  
ToolStripButton tsDESC = new ToolStripButton();  

tsASC.Text = "Sort Asc";  
tsDESC.Text = "Sort Desc";  

tsASC.Image = global::C1FlexGrid\_CustomFilterMenu\_CS.Properties.Resources.Upimage;  
tsDESC.Image = global::C1FlexGrid\_CustomFilterMenu\_CS.Properties.Resources.Downimage;  

tsASC.ImageScaling = ToolStripItemImageScaling.None;  
tsASC.ImageTransparentColor = Color.White;  

tsDESC.ImageScaling = ToolStripItemImageScaling.None;  
tsDESC.ImageTransparentColor = Color.White;  

tsASC.Click += tsASC_Click;  
tsDESC.Click += tsDESC_Click;  

ts.Items.Add(tsASC);  
ts.Items.Add(tsDESC);  
}  

}  

frm.Refresh();  
}  
}  
}  

private void tsASC_Click(object sender, EventArgs e)  
{  
C1FlexGrid1.Sort(SortFlags.Ascending, col);  
filterForm.Close();  
}  

private void tsDESC_Click(object sender, EventArgs e)  
{  
C1FlexGrid1.Sort(SortFlags.Descending, col);  
filterForm.Close();  
}  

Now you have a full-fledged filter and sorting dropdown menu. Download Sample - CS Download Sample - VB