This example illustrates how to search and filter the empty rows in WinForms DataGrid?
WinForms DataGrid (SfDataGrid) allows you to search and filter empty rows by customizing the SearchController.
public Form1()
{
InitializeComponent();
sfDataGrid1.SearchController = new SearchControllerExt(sfDataGrid1, panel);
}
public class SearchControllerExt : SearchController
{
SearchPanel searchPanel;
public SearchControllerExt(SfDataGrid grid, SearchPanel panel) :
base(grid)
{
searchPanel = panel;
this.AllowHighlightSearchText = false;
}
protected override bool FilterRecords(object dataRow)
{
if (this.Provider == null)
Provider = this.DataGrid.View.GetPropertyAccessProvider();
if (searchPanel.chkEmptyRows.Checked)
{
for (int i = 0; i < this.DataGrid.Columns.Count; i++)
{
var col = this.DataGrid.Columns[i];
var cellvalue = Provider.GetFormattedValue(dataRow, col.MappingName);
if (cellvalue == null)
continue;
else
return false;
}
return true;
}
else if (searchPanel.chkNonEmpty.Checked)
{
for (int i = 0; i < this.DataGrid.Columns.Count; i++)
{
var col = this.DataGrid.Columns[i];
var cellvalue = Provider.GetFormattedValue(dataRow, col.MappingName);
if (cellvalue != null)
continue;
else
return false;
}
return true;
}
else if(searchPanel.chkEmptyCell.Checked)
{
for (int i = 0; i < this.DataGrid.Columns.Count; i++)
{
var col = this.DataGrid.Columns[i];
var cellvalue = Provider.GetFormattedValue(dataRow, col.MappingName);
if (cellvalue == null)
return true;
else
continue;
}
return false;
}
else
return base.FilterRecords(dataRow);
}
}
Take a moment to peruse the WinForms DataGrid - Search documentation, where you can find about Search in DataGrid, with code examples.
Visual Studio 2015 and above versions.