GridViewDataColumn Filtering

1 Answer 429 Views
GridView
Ohad
Top achievements
Rank 3
Bronze
Iron
Iron
Ohad asked on 29 Mar 2023, 07:37 AM | edited on 30 Mar 2023, 11:51 AM

I have a GridViewDataColumn and I want to add a filter option to this column.

The value of this column is if of type enum.

I would appreciate help with how to add filtering to this column.

<telerik:GridViewDataColumn Header="Status" CellStyle="{StaticResource StatusCellStyle}"/>

        <Style x:Key="StatusCellStyle" TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <TextBlock Text="{Binding Status}" >
                            <TextBlock.Style>
                                <Style TargetType="TextBlock">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Status}" Value="{x:Static Status.Completed}">
                                            <Setter Property="Foreground" Value="Green"/>
                                        </DataTrigger>

                                        <DataTrigger Binding="{Binding Status}" Value="{x:Static Status.Pending}">
                                            <Setter Property="Foreground" Value="Red"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBlock.Style>
                        </TextBlock>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

1 Answer, 1 is accepted

Sort by
0
Stenly
Telerik team
answered on 31 Mar 2023, 10:41 AM

Hello Ohad,

With the setup that you have provided, to apply filtering on the column, you could utilize its FilterMemberPath property and pass, for example, the Status property as its value.

<telerik:GridViewDataColumn FilterMemberPath="Status"/>

On a side note, I have noticed that a custom ControlTemplate is applied to the GridViewCell elements in the CellStyle of the column. By doing so, the GridViewCell element will lose part of its default functionality, such as editing the displayed value.

Please allow me to provide an alternative approach to the setup of the column, so that it supports out-of-the-box grouping, filtering, and sorting the enum value while keeping the different foreground of the content, depending on its value. To do so, set the DataMemberBinding property of the column and bind it to the Status property. In the CellStyle style, add a new Setter for the Foreground property and bind it to the Status one. Via a converter, you could dictate the color of the text depending on its value. 

The following code snippets show this suggestion's implementation:

<telerik:RadGridView.Columns>
    <telerik:GridViewDataColumn DataMemberBinding="{Binding Status}">
        <telerik:GridViewDataColumn.CellStyle>
            <!--If NoXaml assemblies are used base the style on the default one: BasedOn="{StaticResource GridViewCellStyle}"-->
            <Style TargetType="telerik:GridViewCell">
                <Setter Property="Foreground" Value="{Binding Status, Converter={StaticResource EnumToForegroundConverter}}"/>
            </Style>
        </telerik:GridViewDataColumn.CellStyle>
    </telerik:GridViewDataColumn>
</telerik:RadGridView.Columns>

Where the EnumToForegroundConverter is implemented is as follows:

public class EnumToForegroundConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((Status)value == Status.Completed)
        {
            return Brushes.Green;
        }

        return Brushes.Red;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

This will produce the following result and the column will directly support filtering, sorting, and grouping without specifying the FilterMemberPath property:

With this being said, could you give these suggestions a try?

Regards,
Stenly
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Ohad
Top achievements
Rank 3
Bronze
Iron
Iron
commented on 03 Apr 2023, 08:02 AM | edited

Thank you for the answer

When I override the Cell Style according to your approach, I lose the option to filter

Any idea why this is happening?

Thanks

Stenly
Telerik team
commented on 04 Apr 2023, 03:29 PM

Hello Ohad,

I am not sure why this is occurring on your end. May I ask if you could give the attached sample project a try and see if this behavior is also reproducible in it?

Tags
GridView
Asked by
Ohad
Top achievements
Rank 3
Bronze
Iron
Iron
Answers by
Stenly
Telerik team
Share this question
or