This is a migrated thread and some comments may be shown as answers.
Conditionally format backcolor of Summary Row
1 Answer 39 Views
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 1
Mark asked on 19 Oct 2017, 05:11 PM

I need to format the background color of the Summary row based on its Aggregated (Sum) value compared to another value on the form.  If the values are equal, I want it blue, if they are not equal, I need it to be red.  


            Dim ugRow As GridViewRowInfo
            Dim dAdditionTotal As Double = 0
            For Each ugRow In radGVBuildingAdditions.Rows
                dAdditionTotal += ugRow.Cells("Addition_SQFT").Value

            With Me.radGVBuildingAdditions.SummaryRowsBottom
                If .Count > 0 Then
                    For i As Integer = 0 To .Count - 1
                    Next i
                End If
            End With

            If dAdditionTotal > 0 Then

                Dim summary1 As New GridViewSummaryItem()
                summary1.Name = "Addition_SqFt"
                summary1.Aggregate = GridAggregateFunction.Sum
                summary1.FormatString = "Addition Total SF {0:#,##0}"
                Dim summaryRowItem As New GridViewSummaryRowItem()

                Me.radGVBuildingAdditions.MasterTemplate.ShowTotals = True
                Me.radGVBuildingAdditions.MasterView.SummaryRows(0).IsVisible = True
                Dim dBuildingArea As Double = IIf(CLIB_MAPPS.Utilities.ConvertNULLtoDouble(Me.txtBldgNetSF.Text) > 0, CLIB_MAPPS.Utilities.ConvertNULLtoDouble(Me.txtBldgNetSF.Text), Me.txtBldgEstimatedSF.Text)
                If dAdditionTotal = dBuildingArea Then  ' dBuildingArea from another control on the form
                End If

                Me.radGVBuildingAdditions.MasterTemplate.ShowTotals = False
            End If


I attempted to do it in the ViewCellFormatting event, but this requires parsing the number back out of the formatted text and is only working with the Cell in which I placed the total.


    Private Sub radGVBuildingAdditions_ViewCellFormatting(sender As Object, e As CellFormattingEventArgs) Handles radGVBuildingAdditions.ViewCellFormatting
        If TypeOf e.CellElement Is GridSummaryCellElement Then
            Dim dArea As Double = IIf(CLIB_MAPPS.Utilities.ConvertNULLtoDouble(Me.txtBldgNetSF.Text) > 0, CLIB_MAPPS.Utilities.ConvertNULLtoDouble(Me.txtBldgNetSF.Text), Me.txtBldgEstimatedSF.Text)
            If e.CellElement.ColumnInfo.Name = "Addition_SqFt" Then  
                If dArea = CInt(e.CellElement.Value) Then   'I HAVE TO PARSE THE TOTAL BACK OUT OF e.CellElement.Value
                    e.CellElement.BackColor = Color.DodgerBlue
                    e.CellElement.BackColor = Color.Firebrick
                End If
            End If
        End If
    End Sub


is there a better way to do this?

1 Answer, 1 is accepted

Sort by
Telerik team
answered on 20 Oct 2017, 03:37 PM
Hello Mark,

Thank you for writing.

In order to accomplish your task, you will need to handle the ViewCellFormatting event and parse the value in order the make the validation. Since the event is firing for the summary cells and you need to change the color of the row, you will need to access the row element through the cell.

Additionally, you will also need to cache the already formatted rows as the event will be raised for other cells within the same row and only reset the value for rows which are not formatted. If you will be formatting all of your summary rows then you can skip the caching and the reset: 
public partial class Form2 : Form
    public Form2()
        this.radGridView1.EnableFiltering = true;
        this.radGridView1.DataSource = GetData();
        this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
        GridViewSummaryRowItem summaryRowItemSum = new GridViewSummaryRowItem();
        summaryRowItemSum.Add(new GridViewSummaryItem("Id", "{0:F0}", GridAggregateFunction.Sum));
        summaryRowItemSum = new GridViewSummaryRowItem();
        summaryRowItemSum.Add(new GridViewSummaryItem("Value", "{0:F0}", GridAggregateFunction.Avg));
        this.radGridView1.ViewCellFormatting += RadGridView1_ViewCellFormatting;
    HashSet<GridViewRowInfo> rows = new HashSet<GridViewRowInfo>();
    private void RadGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
        GridSummaryCellElement summaryCell = e.CellElement as GridSummaryCellElement;
        if (summaryCell == null)
        int value = -1;
        if (!string.IsNullOrEmpty(summaryCell.Text) && int.TryParse(summaryCell.Text, out value) && value < 100)
            summaryCell.RowElement.DrawFill = true;
            summaryCell.RowElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
            summaryCell.RowElement.BackColor = Color.LightBlue;
        else if(!rows.Contains(summaryCell.RowInfo))
            summaryCell.RowElement.ResetValue(LightVisualElement.DrawFillProperty, Telerik.WinControls.ValueResetFlags.Local);
            summaryCell.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, Telerik.WinControls.ValueResetFlags.Local);
            summaryCell.RowElement.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local);
    private object GetData()
        DataTable dt = new DataTable();
        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Date", typeof(DateTime));
        dt.Columns.Add("Bool", typeof(bool));
        dt.Columns.Add("Value", typeof(int));
        for (int i = 1; i < 10; i++)
            dt.Rows.Add(i, "Name " + i, DateTime.Now.AddDays(i), i % 2 == 0, Math.Truncate(Convert.ToDouble(DateTime.Now.Millisecond / i)));
        return dt;

I am also attaching a screenshot showing the result on my end.

I hope this helps. Should you have further questions please do not hesitate to write back.

Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Asked by
Top achievements
Rank 1
Answers by
Telerik team
Share this question