DataGridUI.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace ProjectManagementSystem.UI
  12. {
  13. public partial class DataGridUI : UserControl, IDisplayUI
  14. {
  15. private bool needResize = true;
  16. private BindingSource bindingSource = new BindingSource();
  17. public DataGridUI()
  18. {
  19. InitializeComponent();
  20. DoubleBufferedDataGirdView(dataGridView1, true);
  21. dataGridView1.SelectionChanged += (s, e) =>
  22. {
  23. dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
  24. };
  25. this.dataGridView1.DataSource = bindingSource;
  26. }
  27. private void DoubleBufferedDataGirdView(DataGridView dgv, bool flag)
  28. {
  29. Type dgvType = dgv.GetType();
  30. PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
  31. pi.SetValue(dgv, flag, null);
  32. }
  33. public virtual bool Init()
  34. {
  35. return true;
  36. }
  37. public virtual void UpdateDisplay()
  38. {
  39. }
  40. public DataTable GetDataTable()
  41. {
  42. DataTable dt = new DataTable();
  43. for (int count = 0; count < dataGridView1.Columns.Count; count++)
  44. {
  45. if (dataGridView1.Columns[count].Visible)
  46. {
  47. DataColumn dc = new DataColumn(dataGridView1.Columns[count].HeaderText.ToString());
  48. dt.Columns.Add(dc);
  49. }
  50. }
  51. for (int count = 0; count < dataGridView1.Rows.Count; count++)
  52. {
  53. DataRow dr = dt.NewRow();
  54. int colCount = 0;
  55. for (int countsub = 0; countsub < dataGridView1.Columns.Count; countsub++)
  56. {
  57. if (dataGridView1.Columns[countsub].Visible)
  58. {
  59. dr[colCount] = dataGridView1.Rows[count].Cells[countsub].Value.ToString();
  60. colCount++;
  61. }
  62. }
  63. dt.Rows.Add(dr);
  64. }
  65. return dt;
  66. }
  67. public string GetCurrentColValue(string colName)
  68. {
  69. if (dataGridView1.CurrentRow == null)
  70. {
  71. return null;
  72. }
  73. if (!dataGridView1.Columns.Contains(colName))
  74. {
  75. return null;
  76. }
  77. int DGVCurrentRow = dataGridView1.CurrentRow.Index;
  78. if (DGVCurrentRow < 0 || dataGridView1.Rows[DGVCurrentRow].Cells[colName].Value == null)
  79. {
  80. return null;
  81. }
  82. string strOrderID = dataGridView1.Rows[DGVCurrentRow].Cells[colName].Value.ToString();
  83. return strOrderID;
  84. }
  85. private void UpdateBindingSource<T>(IEnumerable<T> dataList)
  86. {
  87. //clear
  88. var dataListCount = dataList?.Count() ?? 0;
  89. if (dataListCount == 0)
  90. {
  91. if (bindingSource.Count > 0)
  92. {
  93. bindingSource.Clear();
  94. }
  95. return;
  96. }
  97. //fully add
  98. if (bindingSource.Count == 0)
  99. {
  100. foreach (var s in dataList)
  101. {
  102. bindingSource.Add(s);
  103. }
  104. needResize = true;
  105. return;
  106. }
  107. //remove
  108. if (dataListCount < bindingSource.Count)
  109. {
  110. var removeCount = bindingSource.Count - dataListCount;
  111. var lastIndex = bindingSource.Count - 1;
  112. for (int idx = 0; idx < removeCount; idx++)
  113. {
  114. var index = lastIndex - idx;
  115. bindingSource.RemoveAt(index);
  116. }
  117. needResize = true;
  118. }
  119. //add or update
  120. int i = 0;
  121. foreach (var s in dataList)
  122. {
  123. if (bindingSource.Count > i)
  124. {
  125. bindingSource[i] = s;
  126. }
  127. else
  128. {
  129. bindingSource.Add(s);
  130. needResize = true;
  131. }
  132. i++;
  133. }
  134. }
  135. public void ShowDataGrid<T>(IEnumerable<T> dataSource, bool directly = false)
  136. {
  137. this.Invoke(new MethodInvoker(() =>
  138. {
  139. this.SuspendLayout();
  140. if (directly)
  141. {
  142. bindingSource.DataSource = dataSource;
  143. }
  144. else
  145. {
  146. UpdateBindingSource(dataSource);
  147. }
  148. foreach (DataGridViewColumn column in dataGridView1.Columns)
  149. {
  150. if (column.Name.Contains("Time"))
  151. {
  152. column.DefaultCellStyle.Format = "G";
  153. }
  154. if (column.Name.Contains("HEX"))
  155. {
  156. column.DefaultCellStyle.Format = "X2";
  157. }
  158. }
  159. var keyInfo = Common.Config.ExcelConfig.Instance.ColumnKeyInfoDict;
  160. foreach (DataGridViewColumn column in dataGridView1.Columns)
  161. {
  162. if (keyInfo.TryGetValue(column.Name, out var columnKeyInfo))
  163. {
  164. column.HeaderText = columnKeyInfo.HeaderText;
  165. column.Visible = columnKeyInfo.Visible;
  166. if (!string.IsNullOrEmpty(columnKeyInfo.Format))
  167. {
  168. column.DefaultCellStyle.Format = columnKeyInfo.Format;
  169. }
  170. }
  171. else
  172. {
  173. var displayName = typeof(T)?.GetProperty(column.Name)?.GetCustomAttribute<DisplayNameAttribute>()?.DisplayName;
  174. if (!string.IsNullOrEmpty(displayName))
  175. {
  176. column.HeaderText = displayName;
  177. }
  178. //else
  179. //{
  180. // column.Visible = false;
  181. //}
  182. }
  183. }
  184. if (needResize && bindingSource.Count > 0)
  185. {
  186. needResize = false;
  187. dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
  188. }
  189. this.ResumeLayout(false);
  190. }));
  191. }
  192. }
  193. }