FieldsView.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using FastReport.Utils;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. namespace FastReport.FastQueryBuilder
  7. {
  8. internal class FieldsView : DataGridView
  9. {
  10. private List<Field> fields;
  11. public List<Field> Fields
  12. {
  13. get => fields;
  14. set
  15. {
  16. fields = value;
  17. // build a copy of fields list. Do not use DataSource = null; DataSource = value; because of bug in SWF
  18. var newList = new List<Field>();
  19. newList.AddRange(fields);
  20. DataSource = newList;
  21. UpdateGroupedFields();
  22. }
  23. }
  24. public List<Field> Groups { get; set; }
  25. public FieldsView()
  26. {
  27. AllowUserToAddRows = false;
  28. AllowUserToDeleteRows = false;
  29. AllowUserToResizeRows = false;
  30. AutoGenerateColumns = false;
  31. BackgroundColor = SystemColors.Control;
  32. ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
  33. GridColor = SystemColors.Control;
  34. RowHeadersVisible = false;
  35. CellEndEdit += DoCellEndEdit;
  36. MyRes res = new MyRes("Forms,QueryBuilder");
  37. var nameColumn = new DataGridViewTextBoxColumn();
  38. nameColumn.HeaderText = res.Get("Name");
  39. nameColumn.DataPropertyName = "FullName";
  40. nameColumn.ReadOnly = true;
  41. var aliasColumn = new DataGridViewTextBoxColumn();
  42. aliasColumn.HeaderText = res.Get("Alias");
  43. aliasColumn.DataPropertyName = "Alias";
  44. var filterColumn = new DataGridViewTextBoxColumn();
  45. filterColumn.HeaderText = res.Get("Filter");
  46. filterColumn.DataPropertyName = "Filter";
  47. var groupColumn = new DataGridViewCheckBoxColumn();
  48. groupColumn.HeaderText = res.Get("Group");
  49. groupColumn.DataPropertyName = "Group";
  50. var orderColumn = new DataGridViewComboBoxColumn();
  51. orderColumn.HeaderText = res.Get("Order");
  52. orderColumn.DataPropertyName = "Order";
  53. orderColumn.FlatStyle = FlatStyle.Flat;
  54. orderColumn.Items.AddRange(new string[] { "", "Asc", "Desc" });
  55. var funcColumn = new DataGridViewComboBoxColumn();
  56. funcColumn.HeaderText = res.Get("Func");
  57. funcColumn.DataPropertyName = "Func";
  58. funcColumn.FlatStyle = FlatStyle.Flat;
  59. funcColumn.Items.AddRange(new string[] { "", "Avg", "Count", "Max", "Min", "Sum" });
  60. Columns.AddRange(new DataGridViewColumn[] { nameColumn, aliasColumn, filterColumn, groupColumn, orderColumn, funcColumn });
  61. }
  62. protected override void OnSizeChanged(EventArgs e)
  63. {
  64. base.OnSizeChanged(e);
  65. if (Columns.Count > 0)
  66. {
  67. int fixedWidth = 80;
  68. Columns[3].Width = Columns[4].Width = Columns[5].Width = this.LogicalToDevice(fixedWidth);
  69. Columns[0].Width = Columns[1].Width = Columns[2].Width = (Width - this.LogicalToDevice(fixedWidth * 3 + SystemInformation.VerticalScrollBarWidth)) / 3 - 1;
  70. }
  71. }
  72. private void DoCellEndEdit(object sender, DataGridViewCellEventArgs e)
  73. {
  74. UpdateGroupedFields();
  75. }
  76. private void UpdateGroupedFields()
  77. {
  78. if (Groups == null)
  79. return;
  80. // remove
  81. for (int i = 0; i < Groups.Count; i++)
  82. {
  83. if (!Fields.Contains(Groups[i]) || !Groups[i].Group)
  84. {
  85. Groups.RemoveAt(i);
  86. i--;
  87. }
  88. }
  89. // add
  90. foreach (Field f in Fields)
  91. {
  92. if (!Groups.Contains(f) && f.Group)
  93. Groups.Add(f);
  94. }
  95. }
  96. }
  97. }