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

How to check the content of an expression column for conditional formatting?

4 Answers 62 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 16 May 2012, 01:50 PM
Hello!

I have many expression columns in my grid.

In the RowLoaded-Event I want th check, if the result of the expression is lower than 0. If yes, the text (forecast) color should be set to red.
How can I access the result of the expression?

private void gridview1(object sender, RowLoadedEventArgs e)
{
   var myExpressionCellList = e.Row.ChildrenOfType<GridViewCell>.Where(c => c.Column.UniqueName == "eAbwVjKgMon1").toList();
   foreach(GridViewCell cell in myExpressCellList)
   {
      cell. .... ?????
   }
}

I tried different things, but I'm not able to get the value for checking.

What I'm doing wrong? Please help.

Kind regards

Michael

4 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 16 May 2012, 02:03 PM
Hello Michael,

 I would recommend you to use a CellStyleSelector to achieve your goal. Based on the data, you can apply the appropriate style. 

I hope this is helpful.

Kind regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Michael
Top achievements
Rank 1
answered on 21 May 2012, 10:08 AM
Hello!

Sorry that I didn't answered before, but we had a long weekend here in Germany.

I tried your suggestions and I think it would work, but I have a special problem.

I try to explain it abstractly:
My datagrid binds to a business objects called "Absatz". There are 12 properties for holding the sales figures for one year on a monthly basis. For every of the 12 GridViewDataColumns I need in addition 4 GridViewExpressionColumns for calculating abberations. If a abberation is negative, the figure should be displayed in red color. So I use the CellStyleSelector-Property for the Expression-Columns.

The item which is commited to the overridden SelectStyle-method is of the type "Absatz". So I would need to calculate the abberations again in the method for each "abberation calumn". But the method only can return one style for one condition.

I tried to detect which expression column calls the SelectStyle by container.ParentOfTypey<GridViewVolumn>().Name or something like that, so the method only calculates the abberation for the right column. But I don't get the right column?

What I'm doing wrong.

Thank you.

Michael
0
Michael
Top achievements
Rank 1
answered on 21 May 2012, 11:09 AM
Hello!

Perhaps there is a better (shorter code) solution, but I managed it now like this:

public class RadGridNumberStyle : Telerik.Windows.Controls.ConditionalStyleSelector
    {
        public override System.Windows.Style SelectStyle(object item, System.Windows.DependencyObject container)
        {
 
            var theColumn = container as GridViewCellBase;
            if (item is Absatz)
            {
 
                Absatz a = item as Absatz;
 
                switch (theColumn.Column.UniqueName)
                {
                    case "eAbwVjKgMon1":
                        {
                            if ((a.decPlanMon1 - a.decVjIstMon1) < 0)
                            {
                                return NegativerWertStyle;
                            }
                            else
                            {
                                return PositiverWertStyle;
                            }
                        }
                    case "eAbwVjPrMon1":
                        {
                            if ((a.decPlanMon1 - a.decVjIstMon1) < 0)
                            {
                                return NegativerWertStyle;
                            }
                            else
                            {
                                return PositiverWertStyle;
                            }
                        }
                    case "eAbwLjKgMon1":
                        {
                            if ((a.decPlanMon1 - a.decLjIstMon1) < 0)
                            {
                                return NegativerWertStyle;
                            }
                            else
                            {
                                return PositiverWertStyle;
                            }
                        }
 
... and so on!
0
Dimitrina
Telerik team
answered on 21 May 2012, 02:55 PM
Hi,

 In case you would like to use the same CellStyleSelector for all the columns, then you will need to manually handle the recognition of the column.  You have done that.

Another approach would be to use a different selector for each of the columns.

All the best,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Michael
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Michael
Top achievements
Rank 1
Share this question
or