Conditional Formatting Based on Other Rows

5 posts, 0 answers
  1. Graeme
    Graeme avatar
    4 posts
    Member since:
    Nov 2012

    Posted 03 Dec 2012 Link to this post

    Hi,

    I am using Telerik for Winforms version 2012.3.1017.40.  I am using a RadGridView control to display some data.

    The following is always true:
    - First columns is a date
    - All other columns are numeric
    - I do not know at design time how many other columns there will be
    - The data is at regular intervals (e.g. each row is a 2 minute step)

    I am trying to achieve the following functionality:
    For each cell value cell:
        Determine value
        Determine Previous Value
        If difference greater than 5:
            backcolour = green
        else if difference less than -5:
            backcolour = red

    The 'previous value' cannot necessarily look at the previous row, I do not know what custom sorting the user will have done.  Rather the previous value should be taken from the row at the previous  point in time (according to the first column(date)).

    I could achieve this by querying the underlying datasource (a DataTable) on the CellFormatting event, but can't help wandering if there is an easier way!

    Thanks for any assistance.
  2. Graeme
    Graeme avatar
    4 posts
    Member since:
    Nov 2012

    Posted 06 Dec 2012 Link to this post

    bump!
  3. Anton
    Admin
    Anton avatar
    167 posts

    Posted 06 Dec 2012 Link to this post

    Hello Graeme,

    Thank you for writing.

    To achieve the desired behavior you should add an invisible column that will hold the indexes on the rows. This column will hold the correct row indexes and after sorting/filtering/grouping and you can use them to access the previous rows. After that subscribe to the CellFormating event and create your logic for formatting cells. For example:
    void radGridView1_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
    {
        GridViewTemplate template = e.Row.ViewTemplate;
        int index = Convert.ToInt32(e.Row.Cells["Index"].Value);
     
        if (index == 0)
        {
            this.ResetCell(e.CellElement);
            return;
        }
     
        GridViewRowInfo prevRow = template.Rows[index - 1];
     
        if (e.Column.Name == "IntValue" && e.RowIndex > 0 && (int)prevRow.Cells["IntValue"].Value > ((int)e.CellElement.Value + 5))
        {
            e.CellElement.DrawFill = true;
            e.CellElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
            e.CellElement.BackColor = Color.Green;
        }
        else if (e.Column.Name == "IntValue" && e.RowIndex > 0 && (int)prevRow.Cells["IntValue"].Value < ((int)e.CellElement.Value - 5))
        {
            e.CellElement.DrawFill = true;
            e.CellElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
            e.CellElement.BackColor = Color.Red;
        }
        else
        {
            this.ResetCell(e.CellElement);  
        }
    }

    Attached is a sample project that comprises the code above.

    I hope this helps. Should you have any other questions, I will be glad to assist you.

    Greetings,
    Anton
    the Telerik team
    Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
  4. Graeme
    Graeme avatar
    4 posts
    Member since:
    Nov 2012

    Posted 11 Dec 2012 Link to this post

    Thanks - I now have this working.  Next question:

    I have another form that allows the user to change the tolerance level (i.e. the greater than or less than 5 difference).  Once they change this, I cannot find a way to get all cells to repaint in or to redo the cellFormatting and update the colours.

    Any suggestions?
  5. Anton
    Admin
    Anton avatar
    167 posts

    Posted 13 Dec 2012 Link to this post

    Hello Graeme,

    Thank you for writing back.

    You can refresh the grid by calling the Refresh method of the MasterTemplate. For example:
    this.radGridView1.MasterTemplate.Refresh();

    I hope this helps.

    All the best,
    Anton
    the Telerik team
    Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
Back to Top