DeletedEntityGrid.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using FastReport;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using InABox.Database;
  5. using InABox.DynamicGrid;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace PRSServer.Forms
  14. {
  15. class DeletedEntityGrid<T> : DynamicGrid<T>, ISpecificGrid where T : Entity, new()
  16. {
  17. private CoreTable Table { get; set; }
  18. public DeletedEntityGrid(CoreTable table)
  19. {
  20. Table = table;
  21. HiddenColumns.Add(x => x.ID);
  22. Options.BeginUpdate().Clear()
  23. .AddRange(DynamicGridOption.EditRows).EndUpdate();
  24. }
  25. protected override DynamicGridColumns LoadColumns()
  26. {
  27. var result = base.LoadColumns();
  28. var deletionColumns = DeletionData.DeletionColumns<T>();
  29. var columns = new DynamicGridColumns();
  30. foreach(var column in deletionColumns.GetColumns())
  31. {
  32. var resultColumn = result.FirstOrDefault(x => x.ColumnName == column.Property);
  33. if(resultColumn is not null)
  34. {
  35. columns.Add(resultColumn);
  36. }
  37. }
  38. if(columns.Count == 0)
  39. {
  40. columns.Add<T, Guid>(x => x.ID, 0, "ID", "", Alignment.MiddleLeft);
  41. }
  42. return columns;
  43. }
  44. protected override void Reload(Filters<T> criteria, Columns<T> columns, ref SortOrder<T>? sort, Action<CoreTable?, Exception?> action)
  45. {
  46. try
  47. {
  48. action(Table, null);
  49. }
  50. catch(Exception e)
  51. {
  52. action(null, e);
  53. }
  54. }
  55. protected override T LoadItem(CoreRow row)
  56. {
  57. return Table.Rows[row.Index].ToObject<T>();
  58. }
  59. protected override void DoEdit()
  60. {
  61. if (!SelectedRows.Any())
  62. return;
  63. var item = LoadItem(SelectedRows.First());
  64. var editor = new DynamicEditorForm(typeof(T));
  65. editor.ReadOnly = true;
  66. editor.OnCustomiseColumns += Editor_OnCustomiseColumns;
  67. editor.OnDefineLookups += sender => DefineLookups(sender, Array.Empty<T>());
  68. editor.Items = new BaseObject[] { item };
  69. editor.ShowDialog();
  70. }
  71. private DynamicGridColumns Editor_OnCustomiseColumns(object sender, DynamicGridColumns? source)
  72. {
  73. var columns = new DynamicGridColumns();
  74. columns.ExtractColumns(typeof(T));
  75. var deletionColumns = DeletionData.DeletionColumns<T>();
  76. columns.RemoveAll(x => !deletionColumns.GetColumns().Any(y => y.Property == x.ColumnName));
  77. return columns;
  78. }
  79. public override void SaveItem(T item)
  80. {
  81. // No saving allowed
  82. }
  83. protected override void DeleteItems(params CoreRow[] rows)
  84. {
  85. // No deleting allowed
  86. }
  87. }
  88. }