This is a migrated thread and some comments may be shown as answers.

Using CellTemplateSelector with GridViewExpressionColumn

3 Answers 184 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Edwin
Top achievements
Rank 1
Edwin asked on 21 Nov 2012, 10:22 PM

Hello,

I am using CellTemplateSelector to change the color of a cell depending on its value.  I found sample code on how to do this and it works well for GridViewDataColumn but I can't get it to work with GridViewExpressionColumn

 
I'm using the following and it works:


<
telerik:GridViewDataColumn DataMemberBinding= "{Binding TotalAdjRateNew}" TextAlignment="Right" Width="100" DataFormatString="{} {0:#%}" IsReadOnly="True">

    <telerik:GridViewDataColumn.Header>

        <TextBlock Text="New Total Adj %" TextWrapping="NoWrap" TextAlignment="Center" />

    </telerik:GridViewDataColumn.Header>

    <telerik:GridViewDataColumn.CellTemplateSelector>

        <telerik:ConditionalDataTemplateSelector>

            <telerik:DataTemplateRule Condition="TotalAdjRateNew &lt; 0">

                <DataTemplate>

                    <TextBlock Text="{Binding TotalAdjRateNew, StringFormat=p0}" 
                        HorizontalAlignment
="Right" Foreground="Red"/>

                </DataTemplate>

            </telerik:DataTemplateRule>
        </telerik:ConditionalDataTemplateSelector>

    </telerik:GridViewDataColumn.CellTemplateSelector>

</telerik:GridViewDataColumn>

 

However when I use the code below with a GridViewExpressionColumn  the value is not displaying.  I am not sure what the binding should be inside the DataTemplateRule.

 

<telerik:GridViewExpressionColumn UniqueName="TotalAdjPCT" TextAlignment="Right" Width="100" 
    DataFormatString
="{} {0:#%}" Expression="MAN_ADJ_PCT + MOD_ADJ_PCT" IsReadOnly="True">

     <telerik:GridViewExpressionColumn.Header>

        <TextBlock Text="Total Adj %" TextWrapping="NoWrap" TextAlignment="Center" />

    </telerik:GridViewExpressionColumn.Header>

    <telerik:GridViewExpressionColumn.CellTemplateSelector>

        <telerik:ConditionalDataTemplateSelector>

            <telerik:DataTemplateRule Condition="MAN_ADJ_PCT + MOD_ADJ_PCT &lt; 0">

                <DataTemplate>

                    <TextBlock Text="{Binding TotalAdjPCT, StringFormat=p0}" HorizontalAlignment="Right" 
                        Foreground
="Red"/>

                </DataTemplate>

            </telerik:DataTemplateRule>

        </telerik:ConditionalDataTemplateSelector>

    </telerik:GridViewExpressionColumn.CellTemplateSelector>

</telerik:GridViewExpressionColumn>

 

3 Answers, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 26 Nov 2012, 03:58 PM
Hi Edwin,

In order to achieve your goal you can check this help article, where is described how to apply a different data template to RadGridView cells using the CellTemplateSelector property. Note that this will be a little bit different when using GridViewExpressionColumn. I have prepared a sample project that shows you the approach. You can find it attached.

Kind regards,
Yoan
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Edwin
Top achievements
Rank 1
answered on 28 Nov 2012, 08:07 PM
Thank you.  This should work.  Except I neglected to mention that the expression column is part of child grid.  I am not sure how to reference the child grid in the code below:


Expression<

 

Func<Club, double>> expression = club => club.StadiumCapacity * 2;

 

GridViewExpressionColumn column =

 

this.clubsGrid.Columns["DoubleValue"] as GridViewExpressionColumn;

 

column.Expression = expression;

Your further assistance would be greatly appreciated.

0
Yoan
Telerik team
answered on 03 Dec 2012, 04:55 PM
Hello Edwin,

If you have GridView with RowDetails, you can subscribe to your child grid's Loaded event and define the ExpressionColumn like so:

private void playersGrid_Loaded(object sender, RoutedEventArgs e)
        {
            var grid = sender as RadGridView;
            if (grid != null)
            {
                Expression<Func<Player, double>> expression = player => player.Number * 2;
                GridViewExpressionColumn column = grid.Columns["DoubleNumber"] as GridViewExpressionColumn;
 
                if (column != null)
                {
                    column.Expression = expression;
                }
            }
        }

Kind regards,
Yoan
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
GridView
Asked by
Edwin
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Edwin
Top achievements
Rank 1
Share this question
or