I need to change the back color of a xrTableCell
based on the value inside.
Say, if the value is "Approved", the cell will be in green background, red for "Disapproved", yellow for "Pending".
In Access database or SQL Reporting (SSRS), we can input =IIF(...)
or =someFunction()
in the BackColor
property. But here in XtraReport, it doesn't allow me.
As for the formatting rules sheet, is it possible for us to write something like [this] == 'Approved'
inside the Condition
?
The whole point is to add formatting rules programmingly.
In my case, there are 10 sections, each of which has status values of "Approved" "Pending" "Disapproved" "Not Started".
If there were a IIF
statement that can be used in the Style setting, I would be willing to do it by hand. But here in DevExpress XtraReport, there is no way to do that. If the rule can only be written as [condition: Approved, then style: green background in that cell], then there will be four rules added to each section, which makes the total of 40 rules! No one would love to do it by hand.
Okay, here comes my solution.
Go to the code-behind of the report, right after InitializeComponent();
we can add code to manipulate the report. Here is my method AddColorCodingFormattingRules(sections, statusColors);
private void AddColorCodingFormattingRules(IEnumerable sections, Dictionary<string, Color> statusColors)
{
foreach (var s in sections)
{
var cellName = string.Format("xrTableCell{0}", s);
var cell = FindControl(cellName, false) as XRTableCell;
if (cell == null) continue;
foreach (var pair in statusColors)
{
var rule = new FormattingRule
{
Condition = string.Format("[{1}] == \'{0}\'", pair.Key, s),
Name = string.Format("_formatStatus{1}{0}", pair.Key, s)
};
rule.Formatting.BackColor = pair.Value;
FormattingRuleSheet.Add(rule);
cell.FormattingRules.Add(rule);
}
}
}
Hope the solution can help someone in need in the future. And hope XtraReport can improve in future versions so that we don't need to be that creative.