DynamicGridColumns.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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>, GlobalConfigurationSettings, UserConfigurationSettings
  10. {
  11. public string GridName { get; set; }
  12. private static Dictionary<Type, List<DynamicGridColumn>> _columnscache = new Dictionary<Type, List<DynamicGridColumn>>();
  13. public void ExtractColumns(Type type, string prefix)
  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))
  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. }
  38. public DynamicGridColumn Add<TType, TProperty>(Expression<Func<TType, TProperty>> member, int width, string caption, string format,
  39. Alignment alignment)
  40. {
  41. var name = CoreUtils.GetFullPropertyName(member, ".");
  42. var result = new DynamicGridColumn
  43. {
  44. ColumnName = name,
  45. Caption = caption,
  46. Width = width,
  47. Format = format,
  48. Alignment = alignment
  49. };
  50. Add(result);
  51. return result;
  52. }
  53. }
  54. }