UCTransfer.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 2019-10-10
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCTransfer.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.ComponentModel;
  19. using System.Drawing;
  20. using System.Data;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Windows.Forms;
  24. namespace HZH_Controls.Controls
  25. {
  26. /// <summary>
  27. /// Class UCTransfer.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. [DefaultEvent("Transfered")]
  32. public partial class UCTransfer : UserControl
  33. {
  34. /// <summary>
  35. /// 移动数据事件
  36. /// </summary>
  37. [Description("移动数据事件"), Category("自定义")]
  38. public event TransferEventHandler Transfered;
  39. /// <summary>
  40. /// The left columns
  41. /// </summary>
  42. private DataGridViewColumnEntity[] leftColumns;
  43. /// <summary>
  44. /// Gets or sets the left columns.
  45. /// </summary>
  46. /// <value>The left columns.</value>
  47. [Description("左侧列表列"), Category("自定义")]
  48. public DataGridViewColumnEntity[] LeftColumns
  49. {
  50. get { return leftColumns; }
  51. set
  52. {
  53. leftColumns = value;
  54. this.dgvLeft.Columns = leftColumns.ToList();
  55. }
  56. }
  57. /// <summary>
  58. /// The right columns
  59. /// </summary>
  60. private DataGridViewColumnEntity[] rightColumns;
  61. /// <summary>
  62. /// Gets or sets the right columns.
  63. /// </summary>
  64. /// <value>The right columns.</value>
  65. [Description("右侧列表列"), Category("自定义")]
  66. public DataGridViewColumnEntity[] RightColumns
  67. {
  68. get { return rightColumns; }
  69. set
  70. {
  71. rightColumns = value;
  72. this.dgvRight.Columns = rightColumns.ToList();
  73. }
  74. }
  75. /// <summary>
  76. /// The left data source
  77. /// </summary>
  78. private object[] leftDataSource;
  79. /// <summary>
  80. /// 左右列表必须设置相同类型的数据源列表,如果为空必须为长度为0的数组
  81. /// </summary>
  82. /// <value>The left data source.</value>
  83. [Description("左侧列表数据源"), Category("自定义"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
  84. public object[] LeftDataSource
  85. {
  86. get { return leftDataSource; }
  87. set
  88. {
  89. leftDataSource = value;
  90. dgvLeft.DataSource = value;
  91. }
  92. }
  93. /// <summary>
  94. /// The right data source
  95. /// </summary>
  96. private object[] rightDataSource;
  97. /// <summary>
  98. /// 左右列表必须设置相同类型的数据源列表,如果为空必须为长度为0的数组
  99. /// </summary>
  100. /// <value>The left data source.</value>
  101. [Description("右侧列表数据源"), Category("自定义"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
  102. public object[] RightDataSource
  103. {
  104. get { return rightDataSource; }
  105. set
  106. {
  107. rightDataSource = value;
  108. dgvRight.DataSource = value;
  109. }
  110. }
  111. /// <summary>
  112. /// Initializes a new instance of the <see cref="UCTransfer"/> class.
  113. /// </summary>
  114. public UCTransfer()
  115. {
  116. InitializeComponent();
  117. LeftColumns = new DataGridViewColumnEntity[0];
  118. RightColumns = new DataGridViewColumnEntity[0];
  119. LeftDataSource = new object[0];
  120. RightDataSource = new object[0];
  121. }
  122. /// <summary>
  123. /// Handles the BtnClick event of the btnRight control.
  124. /// </summary>
  125. /// <param name="sender">The source of the event.</param>
  126. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  127. /// <exception cref="System.Exception">
  128. /// 左右数据源列表不能为空
  129. /// or
  130. /// 左右数据源列表类型不一致,无法进行操作
  131. /// </exception>
  132. private void btnRight_BtnClick(object sender, EventArgs e)
  133. {
  134. if (LeftDataSource == null || RightDataSource == null)
  135. {
  136. throw new Exception("左右数据源列表不能为空");
  137. }
  138. if (LeftDataSource.GetType() != RightDataSource.GetType())
  139. {
  140. throw new Exception("左右数据源列表类型不一致,无法进行操作");
  141. }
  142. if (dgvLeft.SelectRows == null || dgvLeft.SelectRows.Count <= 0)
  143. return;
  144. List<object> lst = new List<object>();
  145. dgvLeft.SelectRows.ForEach(p =>
  146. {
  147. lst.Add(p.DataSource);
  148. p.IsChecked = false;
  149. });
  150. var lstRight = RightDataSource.ToList();
  151. lstRight.AddRange(lst);
  152. var lstLeft = LeftDataSource.ToList();
  153. lstLeft.RemoveAll(p => lst.Contains(p));
  154. RightDataSource = lstRight.ToArray();
  155. LeftDataSource = lstLeft.ToArray();
  156. if (Transfered != null)
  157. {
  158. Transfered(this, new TransferEventArgs() { TransferDataSource = lst.ToArray(), ToRightOrLeft = true });
  159. }
  160. }
  161. /// <summary>
  162. /// Handles the BtnClick event of the btnLeft control.
  163. /// </summary>
  164. /// <param name="sender">The source of the event.</param>
  165. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  166. /// <exception cref="System.Exception">
  167. /// 左右数据源列表不能为空
  168. /// or
  169. /// 左右数据源列表类型不一致,无法进行操作
  170. /// </exception>
  171. private void btnLeft_BtnClick(object sender, EventArgs e)
  172. {
  173. if (LeftDataSource == null || RightDataSource == null)
  174. {
  175. throw new Exception("左右数据源列表不能为空");
  176. }
  177. if (LeftDataSource.GetType() != RightDataSource.GetType())
  178. {
  179. throw new Exception("左右数据源列表类型不一致,无法进行操作");
  180. }
  181. if (dgvRight.SelectRows == null || dgvRight.SelectRows.Count <= 0)
  182. return;
  183. List<object> lst = new List<object>();
  184. dgvRight.SelectRows.ForEach(p =>
  185. {
  186. lst.Add(p.DataSource);
  187. p.IsChecked = false;
  188. });
  189. var lstLeft = LeftDataSource.ToList();
  190. lstLeft.AddRange(lst);
  191. var lstRight = RightDataSource.ToList();
  192. lstRight.RemoveAll(p => lst.Contains(p));
  193. RightDataSource = lstRight.ToArray();
  194. LeftDataSource = lstLeft.ToArray();
  195. if (Transfered != null)
  196. {
  197. Transfered(this, new TransferEventArgs() { TransferDataSource = lst.ToArray(), ToRightOrLeft = false });
  198. }
  199. }
  200. }
  201. }