-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConditionalFormatHelper.cs
More file actions
65 lines (54 loc) · 3.11 KB
/
Copy pathConditionalFormatHelper.cs
File metadata and controls
65 lines (54 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using DevExpress.Blazor;
using DevExpress.Data.Filtering.Helpers;
namespace ConditionalFormatSelector.Data {
public class ConditionalFormatHelper {
public static List<FormatStyleInfo> GetFormatStyles() {
return new List<FormatStyleInfo> {
new FormatStyleInfo("Red Background", "cf-background cf-background-red"),
new FormatStyleInfo("Green Background", "cf-background cf-background-green"),
new FormatStyleInfo("Yellow Background", "cf-background cf-background-yellow"),
new FormatStyleInfo("Red Text", "cf-text cf-text-red"),
new FormatStyleInfo("Green Text", "cf-text cf-text-green")
};
}
public static bool Evaluate(SimpleFormat format, GridCustomizeElementEventArgs args) {
switch(format.Condition.ConditionType) {
case ConditionType.Filter:
return EvaluateFilterCriteria(format, args);
case ConditionType.GreaterThanTotalSummary:
return EvaluateGreaterThanTotalSummary(format, args);
case ConditionType.LessThanTotalSummary:
return EvaluateLessThanTotalSummary(format, args);
default:
return false;
}
}
public static bool EvaluateFilterCriteria(SimpleFormat format, GridCustomizeElementEventArgs args) {
if(format.Condition is not FilterConditionInfo filterInfo)
return false;
object dataItem = args.Grid.GetDataItem(args.VisibleIndex);
var evaluator = new ExpressionEvaluator(
new EvaluatorContextDescriptorDefault(dataItem.GetType()),
filterInfo.FilterCriteria
);
return (bool)evaluator.Evaluate(dataItem);
}
private static bool TryGetSummaryValues(SimpleFormat format, GridCustomizeElementEventArgs args, out double rowValue, out double summaryValue) {
rowValue = 0;
summaryValue = 0;
if(format.Condition is not SummaryConditionInfo summaryInfo)
return false;
object dataItem = args.Grid.GetDataItem(args.VisibleIndex);
rowValue = Convert.ToDouble(dataItem?.GetType()?.GetProperty(summaryInfo.FieldName)?.GetValue(dataItem));
IGridSummaryItem? totalSummaryItem = args.Grid.GetTotalSummaryItems().FirstOrDefault(item => item.Name == summaryInfo.SummaryName);
if(totalSummaryItem == null)
return false;
summaryValue = Convert.ToDouble(args.Grid.GetTotalSummaryValue(totalSummaryItem));
return true;
}
public static bool EvaluateGreaterThanTotalSummary(SimpleFormat format, GridCustomizeElementEventArgs args) =>
TryGetSummaryValues(format, args, out double rowValue, out double summaryValue) && rowValue > summaryValue;
public static bool EvaluateLessThanTotalSummary(SimpleFormat format, GridCustomizeElementEventArgs args) =>
TryGetSummaryValues(format, args, out double rowValue, out double summaryValue) && rowValue < summaryValue;
}
}