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

Conditional Formatting Based on Other Rows

4 Answers 256 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Graeme
Top achievements
Rank 1
Graeme asked on 03 Dec 2012, 02:49 PM
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.

4 Answers, 1 is accepted

Sort by
0
Graeme
Top achievements
Rank 1
answered on 06 Dec 2012, 11:46 AM
bump!
0
Anton
Telerik team
answered on 06 Dec 2012, 04:04 PM
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.
0
Graeme
Top achievements
Rank 1
answered on 11 Dec 2012, 10:29 AM
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?
0
Anton
Telerik team
answered on 13 Dec 2012, 05:52 PM
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.
Tags
GridView
Asked by
Graeme
Top achievements
Rank 1
Answers by
Graeme
Top achievements
Rank 1
Anton
Telerik team
Share this question
or