How to hide the rows based on condition in WPF DataGrid?
In SfDataGrid, you can hide the rows based on some condition by using QueryRowHeight event.
The following code example explains how to hide the rows based on condition by setting e.Height to zero.
C#
//Hook the QueryRowHeight event this.sfdatagrid.QueryRowHeight += sfdatagrid_QueryRowHeight; //QueryRowHeight event for hide the rows void sfdatagrid_QueryRowHeight(object sender, QueryRowHeightEventArgs e) { var grid = sender as SfDataGrid; var rowData = grid.GetRecordAtRowIndex(e.RowIndex); var provider = grid.View.GetPropertyAccessProvider(); var celltext = provider.GetValue(rowData, "ProductName"); if (celltext != null && celltext.Equals("HardWare")) { e.Height = 0; e.Handled = true; } }
When you need to hide/unhide a row at the run time, you can trigger QueryRowHeight event only for particular row by calling SfDataGrid.InvalidateRowHeight and VisualContainer.InvalidateMeasureInfo methods.
RowValidating event is triggered while editing a particular row. So that you can change the RowHeight based on edited values by calling SfDataGrid.InvalidateRowHeight and VisualContainer.InvalidateMeasureInfo methods.
C#
//Hook the RowValidating event this.sfdatagrid.RowValidating += sfdatagrid_RowValidating; // RowValidating event void sfdatagrid_RowValidating(object sender, RowValidatingEventArgs args) { sfdatagrid.InvalidateRowHeight(args.RowIndex); this.sfdatagrid.GetVisualContainer().InvalidateMeasureInfo(); }
You can bring the items which are hidden into View when property value changes by invalidating the row height by handling RecordPropertyChanged event.
C#
//Hook the ItemsSourceChanged event this.sfdatagrid.ItemsSourceChanged += Sfdatagrid_ItemsSourceChanged; //ItemsSourceChanged event for unhide the hidden rows void Sfdatagrid_ItemsSourceChanged(object sender, GridItemsSourceChangedEventArgs e) { if(this.sfdatagrid.View!= null) this.sfdatagrid.View.RecordPropertyChanged += View_RecordPropertyChanged; } void View_RecordPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { var datarowbase = this.sfdatagrid.GetRowGenerator().Items.FirstOrDefault(row => row.RowData == sender); if (datarowbase != null && datarowbase.RowIndex > 0) { sfdatagrid.InvalidateRowHeight(datarowbase.RowIndex); this.sfdatagrid.GetVisualContainer().InvalidateMeasureInfo(); } }
Sample Links:
Conclusion
I hope you enjoyed learning about how to hide the rows based on condition in DataGrid.
You can refer to our WPF 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 WPF
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!