// *********************************************************************** // Assembly : HZH_Controls // Created : 2019-10-10 // // *********************************************************************** // // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com // // // Blog: https://www.cnblogs.com/bfyx // GitHub:https://github.com/kwwwvagaa/NetWinformControl // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git // // If you use this code, please keep this note. // *********************************************************************** using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace HZH_Controls.Controls { /// /// Class UCTransfer. /// Implements the /// /// [DefaultEvent("Transfered")] public partial class UCTransfer : UserControl { /// /// 移动数据事件 /// [Description("移动数据事件"), Category("自定义")] public event TransferEventHandler Transfered; /// /// The left columns /// private DataGridViewColumnEntity[] leftColumns; /// /// Gets or sets the left columns. /// /// The left columns. [Description("左侧列表列"), Category("自定义")] public DataGridViewColumnEntity[] LeftColumns { get { return leftColumns; } set { leftColumns = value; this.dgvLeft.Columns = leftColumns.ToList(); } } /// /// The right columns /// private DataGridViewColumnEntity[] rightColumns; /// /// Gets or sets the right columns. /// /// The right columns. [Description("右侧列表列"), Category("自定义")] public DataGridViewColumnEntity[] RightColumns { get { return rightColumns; } set { rightColumns = value; this.dgvRight.Columns = rightColumns.ToList(); } } /// /// The left data source /// private object[] leftDataSource; /// /// 左右列表必须设置相同类型的数据源列表,如果为空必须为长度为0的数组 /// /// The left data source. [Description("左侧列表数据源"), Category("自定义"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] public object[] LeftDataSource { get { return leftDataSource; } set { leftDataSource = value; dgvLeft.DataSource = value; } } /// /// The right data source /// private object[] rightDataSource; /// /// 左右列表必须设置相同类型的数据源列表,如果为空必须为长度为0的数组 /// /// The left data source. [Description("右侧列表数据源"), Category("自定义"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] public object[] RightDataSource { get { return rightDataSource; } set { rightDataSource = value; dgvRight.DataSource = value; } } /// /// Initializes a new instance of the class. /// public UCTransfer() { InitializeComponent(); LeftColumns = new DataGridViewColumnEntity[0]; RightColumns = new DataGridViewColumnEntity[0]; LeftDataSource = new object[0]; RightDataSource = new object[0]; } /// /// Handles the BtnClick event of the btnRight control. /// /// The source of the event. /// The instance containing the event data. /// /// 左右数据源列表不能为空 /// or /// 左右数据源列表类型不一致,无法进行操作 /// private void btnRight_BtnClick(object sender, EventArgs e) { if (LeftDataSource == null || RightDataSource == null) { throw new Exception("左右数据源列表不能为空"); } if (LeftDataSource.GetType() != RightDataSource.GetType()) { throw new Exception("左右数据源列表类型不一致,无法进行操作"); } if (dgvLeft.SelectRows == null || dgvLeft.SelectRows.Count <= 0) return; List lst = new List(); dgvLeft.SelectRows.ForEach(p => { lst.Add(p.DataSource); p.IsChecked = false; }); var lstRight = RightDataSource.ToList(); lstRight.AddRange(lst); var lstLeft = LeftDataSource.ToList(); lstLeft.RemoveAll(p => lst.Contains(p)); RightDataSource = lstRight.ToArray(); LeftDataSource = lstLeft.ToArray(); if (Transfered != null) { Transfered(this, new TransferEventArgs() { TransferDataSource = lst.ToArray(), ToRightOrLeft = true }); } } /// /// Handles the BtnClick event of the btnLeft control. /// /// The source of the event. /// The instance containing the event data. /// /// 左右数据源列表不能为空 /// or /// 左右数据源列表类型不一致,无法进行操作 /// private void btnLeft_BtnClick(object sender, EventArgs e) { if (LeftDataSource == null || RightDataSource == null) { throw new Exception("左右数据源列表不能为空"); } if (LeftDataSource.GetType() != RightDataSource.GetType()) { throw new Exception("左右数据源列表类型不一致,无法进行操作"); } if (dgvRight.SelectRows == null || dgvRight.SelectRows.Count <= 0) return; List lst = new List(); dgvRight.SelectRows.ForEach(p => { lst.Add(p.DataSource); p.IsChecked = false; }); var lstLeft = LeftDataSource.ToList(); lstLeft.AddRange(lst); var lstRight = RightDataSource.ToList(); lstRight.RemoveAll(p => lst.Contains(p)); RightDataSource = lstRight.ToArray(); LeftDataSource = lstLeft.ToArray(); if (Transfered != null) { Transfered(this, new TransferEventArgs() { TransferDataSource = lst.ToArray(), ToRightOrLeft = false }); } } } }