整個問題是要編程添加格式化規則。
在我的情況下,有10個部分,其中每個部分的狀態值爲「Approved」,「Pending」,「Disapproved」,「Not Started」。
如果有,可以在樣式設置中使用的IIF
聲明,我願意通過做手工。但是在DevExpress XtraReport中,沒有辦法做到這一點。如果規則只能寫成[條件:已批准,然後在該單元格中顯示樣式:綠色背景],則會爲每個部分添加四條規則,這將使總數爲40的規則!沒有人願意親手做。
好的,我的解決方案來了。
轉到報告的代碼隱藏,在InitializeComponent();
之後,我們可以添加代碼來操縱報告。這裏是我的方法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);
}
}
}
希望該解決方案能夠幫助有需要的未來。希望XtraReport在未來的版本中可以有所改進,這樣我們就不需要那些創意。