DeletedEntityGrid.cs 2.9 KB

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