Conditional Formatting based on other Field

6 posts, 0 answers
  1. Jan-Bernd
    Jan-Bernd avatar
    2 posts
    Member since:
    Mar 2013

    Posted 14 Mar 2013 Link to this post

    Hi,

    I have a PivotGrid, which has the following layout:

    <telerik:RadPivotGrid ID="RadPivotGrid1" runat="server" AllowFiltering="false" RowGroupsDefaultExpanded="false"  RowTableLayout="Compact"
        ShowColumnHeaderZone="false" ShowDataHeaderZone="false" ShowFilterHeaderZone="false" ShowRowHeaderZone="false"
        OnNeedDataSource="RadPivotGrid1_NeedDataSource" OnCellDataBound="RadPivotGrid1_CellDataBound">
        <ClientSettings EnableFieldsDragDrop="false">
            <Scrolling AllowVerticalScroll="false"></Scrolling>
        </ClientSettings>
        <TotalsSettings RowsSubTotalsPosition="Last" ColumnsSubTotalsPosition="None" />
        <Fields>
            <telerik:PivotGridRowField DataField="Customer" ZoneIndex="0">
            </telerik:PivotGridRowField>
            <telerik:PivotGridRowField DataField="Contract" ZoneIndex="1">
            </telerik:PivotGridRowField>
            <telerik:PivotGridColumnField DataField="Quater">
            </telerik:PivotGridColumnField>
            <telerik:PivotGridColumnField DataField="SalesTax">
            </telerik:PivotGridColumnField>
            <telerik:PivotGridAggregateField DataField="Amount" Aggregate="Sum">
            </telerik:PivotGridAggregateField>
            <telerik:PivotGridAggregateField DataField="Changed" Aggregate="Sum" IsHidden="true">
            </telerik:PivotGridAggregateField>
        </Fields>
    </telerik:RadPivotGrid>

    The AggregateField "Amount" is type of decimal and "Changed" is type of bool. 

    My aim is to format the Field "Amount" with an ending "*" or a special color, if the Field "Changed" is true. How could I achieve this?


    Thanks,
    Jan-Bernd
  2. Angel Petrov
    Admin
    Angel Petrov avatar
    1006 posts

    Posted 19 Mar 2013 Link to this post

    Hello Jan-Bernd,

    Currently there is no easy way to format an aggregate cell depending on the value of another aggregate cell. However you can try something like this:
    protected void RadPivotGrid2_CellDataBound(object sender, PivotGridCellDataBoundEventArgs e)
        {
            if (e.Cell is PivotGridDataCell)
            {
                PivotGridDataCell cell = e.Cell as PivotGridDataCell;
                if (cell != null && (cell.Field as PivotGridAggregateField).DataField == "Changed")
                {
                    if (Convert.ToBoolean(cell.DataItem))
                    {
                        var cells = (cell.Parent as PivotGridDataItem).Cells;
                        int index = cells.Cast<PivotGridDataCell>().ToList().IndexOf(cell);
                        cells[index-1].CssClass = "YourCssClass";
                    }
                }
        
            }
        }

    In the example provided I am intercepting the OnCellDataBound event and checking whether the cell belongs to the Changed aggregate field. Later given the value of the cell I am assigning a CSS class to the previous cell.

    Kind regards,
    Angel Petrov
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jan-Bernd
    Jan-Bernd avatar
    2 posts
    Member since:
    Mar 2013

    Posted 19 Mar 2013 Link to this post

    Hello Angel,

    thanks for your reply.

    Your code works fine if I set the Field "Changed" to visible but if it's not, the OnCellDataBound won't get fired. I've tried to set the Changed-Field to visible and hide it by CSS-Class but it destroys the layout of the pivotgrid...

    Do you have any other suggestion for me?

    Thanks,
    Jan-Bernd
  5. Angel Petrov
    Admin
    Angel Petrov avatar
    1006 posts

    Posted 22 Mar 2013 Link to this post

    Hi Jan-Bernd,

    I am sorry to say but the behavior you are experiencing is expected. When the Changed field is not visible the OnCellDataBound event is not suppose to fire. As for the layout problem could you please show us how are you applying the CSS and what type of problem are you experiencing? Thus we should be able to give you a suggestion on how to resolve the issue.

    Kind regards,
    Angel Petrov
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  6. Daniel
    Daniel avatar
    4 posts
    Member since:
    Feb 2013

    Posted 02 Jun in reply to Angel Petrov Link to this post

    I would like to do a very similar thing to this, except i would like to change the forecolor of the parent row (zoneindex 0) based off a field in the dataset that isnt shown on the chart and add a tooltip to the parent field based on another field in the dataset that isnt on the chart.

     

    is that at all possible?

  7. Angel Petrov
    Admin
    Angel Petrov avatar
    1006 posts

    Posted 07 Jun Link to this post

    Hello,

    Are you using a chart control or a pivot grid?

    If you are using a pivot and the field is not visible the OnCellDataBound event will not fire thus preventing you from styling the row. Considering the aforementioned I suggest revising the requirements.

    Regards,
    Angel Petrov
    Telerik
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017