RadGridView: Style cells of auto generated columns

1 Answer 270 Views
GridView Styling
ffunke
Top achievements
Rank 1
ffunke asked on 29 Mar 2023, 12:37 PM

Hi, 

I am using a RadGridView to bind a DataTable. In that table I have dynamically generated columns. That's why I chose to set AutoGenerateColumns=true. 

I am now looking for a way to style the cells in depending on the properties of the object within the cell of the DataTable.

What I tried until now:

The first method seem the most promissing to me, so here is some code for that:


<UserControl.Resources>
        <Style x:Key="PilotStyle"
               TargetType="telerik:GridViewCell">
            <Setter Property="Background"
                    Value="Magenta" />
        </Style>
        <Style x:Key="ExpansionStyle"
               TargetType="telerik:GridViewCell">
            <Setter Property="Background"
                    Value="Aqua" />
        </Style>
        <valueConverters:RodTimeTypeConverter x:Key="converter" />
        <local:RodTableCellStyleSelector x:Key="selector"
                                         ConditionConverter="{StaticResource converter}">
            <local:RodTableCellStyleSelector.Rules>
                <local:ConditionalStyleRule Style="{StaticResource PilotStyle}">
                    <local:ConditionalStyleRule.Value>
                        <system:Boolean>True</system:Boolean>
                    </local:ConditionalStyleRule.Value>
                </local:ConditionalStyleRule>
                <local:ConditionalStyleRule Style="{StaticResource ExpansionStyle}">
                    <local:ConditionalStyleRule.Value>
                        <system:Boolean>False</system:Boolean>
                    </local:ConditionalStyleRule.Value>
                </local:ConditionalStyleRule>
            </local:RodTableCellStyleSelector.Rules>
        </local:RodTableCellStyleSelector>
    </UserControl.Resources>


<telerik:RadGridView  Grid.Row="1"
                                  CanUserSortColumns="False"
                                  CanUserSelectColumns="False"
                                  CanUserReorderColumns="False"
                                  CanUserDeleteRows="False"
                                  CanUserGroupColumns="False"
                                  CanUserSelect="False"
                                  IsFilteringAllowed="False"
                                  ShowColumnSortIndexes="False"
                                  ShowGroupPanel="False"
                                  RowHeight="45"
                                  AutoGeneratingColumn="GridViewDataControl_OnAutoGeneratingColumn"
                                  ItemsSource="{Binding RodTimesTable.DefaultView}">
                <telerik:RadGridView.Resources>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="FontSize"
                                Value="20" />
                    </Style>

                </telerik:RadGridView.Resources>
            </telerik:RadGridView>


public class RodTableCellStyleSelector : StyleSelector
    {
        public override Style SelectStyle(object item, DependencyObject container)
        {
            var conditionValue = this.ConditionConverter.Convert(item, null, null, null);
            foreach (var rule in this.Rules.Where(rule => Equals(rule.Value, conditionValue)))
            {
                return rule.Style;
            }

            return base.SelectStyle(item, container);
        }

        List<ConditionalStyleRule> rules;
        public List<ConditionalStyleRule> Rules
        {
            get
            {
                return this.rules ??= new List<ConditionalStyleRule>();
            }
        }

        public IValueConverter ConditionConverter { get; set; }
    }
    public class ConditionalStyleRule
    {
        public object Value { get; set; }

        public Style Style { get; set; }
    }



private void GridViewDataControl_OnAutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
        {
            var grid = (RadGridView)sender;
            var dataGrid = (grid.ItemsSource as DataView)?.Table;
            e.Column.Header = dataGrid?.Columns[e.Column.Header.ToString()].Caption;
            if (e.Column.Header != null && (e.Column.Header.ToString().StartsWith("Pilot") || e.Column.Header.ToString().StartsWith("Expansion")) )
            {
                e.Column.CellStyleSelector = (StyleSelector)this.FindResource("selector");
            }
        }

I would really appreciate any help.

 

Thanks in advance.

1 Answer, 1 is accepted

Sort by
0
Accepted
Stenly
Telerik team
answered on 31 Mar 2023, 08:02 AM

Hello Ferdinand,

The item parameter will be of type DataRowView as you have noticed. Through its Row property, you could retrieve the DataRow instance, which exposes the ItemsArray collection. This collection will provide information about the row's cells' values. 

The following code snippet shows the above suggestion's implementation:

public override Style SelectStyle(object item, DependencyObject container)
{
    DataRowView dataRowView = (DataRowView)item;
    DataRow dataRow = dataRowView.Row;
    object?[] items = dataRow.ItemArray;

    //Find the needed value and return the desired Style property
}

With this being said, I have prepared a sample project for you to test.

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.

ffunke
Top achievements
Rank 1
commented on 31 Mar 2023, 01:14 PM

That did the trick. Thanks a lot.
Tags
GridView Styling
Asked by
ffunke
Top achievements
Rank 1
Answers by
Stenly
Telerik team
Share this question
or