DynamicGridColumns.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using InABox.Configuration;
  6. using InABox.Core;
  7. namespace InABox.DynamicGrid
  8. {
  9. public class DynamicGridColumns : List<DynamicGridColumn>, IGlobalConfigurationSettings, UserConfigurationSettings
  10. {
  11. public string GridName { get; set; }
  12. private static Dictionary<Type, List<DynamicGridColumn>> _columnscache = new Dictionary<Type, List<DynamicGridColumn>>();
  13. public DynamicGridColumns ExtractColumns(Type type)
  14. {
  15. if (!_columnscache.ContainsKey(type))
  16. {
  17. _columnscache[type] = new List<DynamicGridColumn>();
  18. var properties = DatabaseSchema.Properties(type).OrderBy(x => x.Sequence);
  19. foreach (var prop in properties)
  20. {
  21. if ((prop.Editor != null && !(prop.Editor is NullEditor)) || prop.Required)
  22. {
  23. var col = new DynamicGridColumn
  24. {
  25. ColumnName = prop.Name,
  26. Width = prop.Editor.Width,
  27. Alignment = prop.Editor.Alignment,
  28. Format = prop.Editor.Format,
  29. Editor = prop.Editor,
  30. Caption = prop.Caption
  31. };
  32. _columnscache[type].Add(col);
  33. }
  34. }
  35. }
  36. AddRange(_columnscache[type]);
  37. return this;
  38. }
  39. public DynamicGridColumn Add<TType, TProperty>(Expression<Func<TType, TProperty>> member, int width, string caption, string format,
  40. Alignment alignment)
  41. {
  42. var name = CoreUtils.GetFullPropertyName(member, ".");
  43. var result = new DynamicGridColumn
  44. {
  45. ColumnName = name,
  46. Caption = caption,
  47. Width = width,
  48. Format = format,
  49. Alignment = alignment
  50. };
  51. Add(result);
  52. return result;
  53. }
  54. }
  55. }