DynamicFormControlGrid.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using InABox.Clients;
  5. using InABox.Core;
  6. namespace InABox.DynamicGrid
  7. {
  8. public class FormControlGrid<T> : DynamicGrid<T> where T : DFLayoutControl, new()
  9. {
  10. public FormControlGrid()
  11. {
  12. Items = new List<T>();
  13. Options.AddRange(DynamicGridOption.RecordCount);
  14. }
  15. public List<T> Items { get; set; }
  16. protected override void DeleteItems(params CoreRow[] rows)
  17. {
  18. var items = new List<DFLayoutControl>();
  19. foreach (var row in rows)
  20. items.Add(Items[row.Index]);
  21. Items.RemoveAll(x => items.Contains(x));
  22. }
  23. protected override T LoadItem(CoreRow row)
  24. {
  25. return Items[row.Index];
  26. }
  27. protected override void Reload(Filters<T> criteria, Columns<T> columns, ref SortOrder<T> sort, Action<CoreTable, Exception> action)
  28. {
  29. var table = new CoreTable();
  30. table.LoadColumns(typeof(T));
  31. table.LoadRows(Items.OrderBy(x => x.Sequence));
  32. action?.Invoke(table, null);
  33. }
  34. protected override void SaveItem(T item)
  35. {
  36. if (!Items.Contains(item))
  37. Items.Add(item);
  38. }
  39. protected override Document LoadDocument(Guid id)
  40. {
  41. Document doc = null;
  42. if (id == Guid.Empty)
  43. doc = new Client<Document>().Load(new Filter<Document>(x => x.ID).IsEqualTo(id)).FirstOrDefault();
  44. if (doc == null)
  45. doc = new Document();
  46. return doc;
  47. }
  48. protected override Document FindDocument(string filename)
  49. {
  50. return new Client<Document>().Load(new Filter<Document>(x => x.FileName).IsEqualTo(filename)).FirstOrDefault();
  51. }
  52. protected override void SaveDocument(Document document)
  53. {
  54. new Client<Document>().Save(document, "");
  55. }
  56. }
  57. }