Xaphann Xaphann - 3 months ago 43
C# Question

WPF hide row in datagrid based on condition

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.

Answer

If hideRow it`s not a field of table:

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding AnyProp, Converter={StaticResource hiddenConverter}}" Value="True">
                <Setter Property="Visibility" Value="Collapsed"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.RowStyle>

And realize Converter with your logic

[ValueConversion(typeof(yourPropType), typeof(bool))]
public class hiddenConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (hideRow == "Yes")
        {
           return true;
        }
        else
        {
           return false;
        }
    }


}
Comments