How to apply filtering for GridCheckBoxSelectorColumn in WinForms DataGrid?
In WinForms DataGrid (SfDataGrid), As of now, we cannot apply filters directly to the GridCheckBoxSelectorColumn. However, we made a customization to apply filters using ViewFilter. You can click the GridCheckBoxSelectorColumn header cell to apply filter. Again, click the header to remove filter.
Kindly refer the below code snippet.
ObservableCollection<object> selectedItems = new ObservableCollection<object>();
sfDataGrid1.CellClick += SfDataGrid1_CellClick;
sfDataGrid1.SelectionChanged += SfDataGrid1_SelectionChanged;
private void SfDataGrid1_SelectionChanged(object sender, Syncfusion.WinForms.DataGrid.Events.SelectionChangedEventArgs e)
{
if(e.AddedItems.Count > 0 && !selectedItems.Contains(e.AddedItems[0]))
selectedItems.Add(e.AddedItems[0]);
if (e.RemovedItems.Count > 0 && selectedItems.Contains(e.RemovedItems[0]))
selectedItems.Remove(e.RemovedItems[0]);
}
bool recordsfiltered = false;
private void SfDataGrid1_CellClick(object sender, Syncfusion.WinForms.DataGrid.Events.CellClickEventArgs e)
{
if (e.DataRow.RowType == Syncfusion.WinForms.DataGrid.Enums.RowType.HeaderRow
&& e.DataColumn.GridColumn is GridCheckBoxSelectorColumn)
{
if (!recordsfiltered)
{
sfDataGrid1.View.Filter = FilterRecords;
recordsfiltered = true;
}
else
{
sfDataGrid1.View.Filter = null;
recordsfiltered = false;
}
sfDataGrid1.View.RefreshFilter();
selectedItems.ForEach(x => this.sfDataGrid1.SelectedItems.Add(x));
}
}
public bool FilterRecords(object o)
{
var item = o as OrderInfo;
if (item != null)
{
if (selectedItems.Contains(item))
{
return true;
}
}
return false;
}
Take a moment to peruse the WinForms DataGrid - ViewFilter documentation, where you can find about the clipboard operations with code examples.
Conclusion
I hope you enjoyed learning about how to apply filtering for GridCheckBoxSelectorColumn in WinForms DataGrid.
You can refer to our WinForms DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms DataGrid example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!