I need to hide rows in datagrid based on parameters and values in the datagrid. I figured to do something like this;
foreach (System.Data.DataRowView dr in myDataGrid.ItemsSource)
{
//Logic to determine if Row should be hidden
if (hideRow == "Yes")
{
//Hide row code
}
}
I just cannot figure how to actual hide the row. Please note I don't want to remove the row form the datagrid or the item source.
Use a CollectionViewSource to link the DataGrid with your business data. The CollectionViewSource fires a filter event for every row. In this event, your code can decide if the row should be displayed.
Add to your XAML:
Add the following to your code behind file:
Create the filter eventhandler. You can get the row data from e.Item. By setting e.Accepted you can control if the row should be displayed.
If hideRow is not a field of the table (i.e. not a column in the DataGridRow):
And realize Converter with your logic. The type of the bound variable, AnyProp above, will be yourPropertyType below. AnyProp could be any of the columns in the row.
'value' will be AnyProp, and it can be used in the logic that determines whether or not to show the row, or that decision can be based on something else entirely, such as 'hideRow' in the example.
VisibilitySetter is a Class that Implements IValueConverter. Here is the Class...
You can do this in Datagrid.ItemContainerStyle instead of doing it in codebehind...