-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyConverter.cs
50 lines (48 loc) · 1.83 KB
/
MyConverter.cs
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
using DevExpress.Spreadsheet;
using System;
using System.Collections.Generic;
using System.Linq;
namespace DataBindingToListExample {
#region #MyWeatherConverter
public class MyWeatherConverter : IBindingRangeValueConverter {
public object ConvertToObject(CellValue value, Type requiredType, int columnIndex) {
if (requiredType == typeof(DateTime))
return value.DateTimeValue;
if (requiredType == typeof(Weather)) {
if (requiredType == typeof(Weather)) {
Weather w;
if (Enum.TryParse(value.TextValue, out w)) return w;
return Weather.Undefined;
}
else
return value.TextValue;
}
if (requiredType == typeof(List<HourlyReport>))
return new List<HourlyReport>();
return value.TextValue;
}
public CellValue TryConvertFromObject(object value) {
if (value is DateTime) {
return ((DateTime)value).ToString("MMM-dd");
}
if (value is Weather) {
return value.ToString();
}
if (value is List<HourlyReport>) {
var hourly = (List<HourlyReport>)value;
if (hourly.Count == 0) return "Undefined";
var high = hourly
.OrderByDescending(p => p.Temperature)
.FirstOrDefault()
.Temperature;
var low = hourly
.OrderBy(p => p.Temperature)
.FirstOrDefault()
.Temperature;
return String.Format("High - {0}, Low - {1}", high, low);
}
return CellValue.TryCreateFromObject(value);
}
}
#endregion #MyWeatherConverter
}