using DevExpress.XtraEditors; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace TProduct.WinFrmUI.Data4Owner { public partial class SelectMotorComboxContentCtrl : DevExpress.XtraEditors.XtraUserControl { public SelectMotorComboxContentCtrl() { InitializeComponent(); this.layoutControl1.SetupLayoutControl(); this.gridViewMain.SetNormalView(30); this.gridViewMain.SetGridMianViewColor(); this.gridViewMain.RegistCustomDrawRowIndicator(); this.gridViewMain.RowClick += new DevExpress.XtraGrid.Views.Grid.RowClickEventHandler(this.GridViewMain_RowClick); } public event Action FocusedDataChangedEvent = null; public event Action AddDataChanedEvent = null; public event Action EditDataChanedEvent = null; private List _bindList = null; private bool _isChangeList = false;//是否修改了数组成员 public bool IsChangeList { get => _isChangeList; set => _isChangeList = value; } private long _SeriesID = 0; public Model.ProductMainExMotor SetBindingData(long SeriesID, long ProductID) { if (SeriesID < 1) return null; _SeriesID = SeriesID; _bindList = new BLL.ProductMotor().GetExBySeriesID(SeriesID); if (_bindList == null || _bindList.Count < 1) { return null; } this.bindingSource1.DataSource = _bindList; this.bindingSource1.ResetBindings(false); this.gridViewMain.RefreshData(); Model.ProductMainExMotor find = null; if (ProductID > 0) { find = this._bindList.Find(x => x.ID == ProductID); } if (find == null) find = _bindList.First(); SetFocusedData(find); this.FocusedDataChangedEvent?.Invoke(find); return find; } /// /// 初始化并设置默认值 /// /// public bool SetFocusedData(Model.ProductMainExMotor sel) { if (_bindList == null) return false; if (sel == null) { return false; } var find_index = this._bindList.FindIndex(x => x.ID == sel.ID); this.gridViewMain.SelectRow(find_index); this.gridViewMain.FocusedRowHandle = find_index; return true; } public void SetReadOnly() { this.barButAdd.Visibility = DevExpress.XtraBars.BarItemVisibility.Never; this.barButEdit.Visibility = DevExpress.XtraBars.BarItemVisibility.Never; } private void GridViewMain_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e) { var row = this.gridViewMain.GetFocusedRow(); if (row == null) return; this.FocusedDataChangedEvent?.Invoke(row as Model.ProductMainExMotor); } private void barButAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { Add(); } private void barButEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { Edit(); } private void barButSearch_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { this.gridViewMain.OptionsFind.AlwaysVisible = this.barCkSelect.Checked; } private void Edit() { var row = this.gridViewMain.GetCurrentViewModel(_bindList); if (row == null) { XtraMessageBox.Show("请选择一行数据!"); return; } WaitFrmHelper.ShowWaitForm(); var dlg = new EditProductMotorDlg(); dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); }; dlg.SetBindingData(row); dlg.ReloadDataEvent += (rhs,ok) => { _isChangeList = true; row.Reset(rhs); this.bindingSource1.ResetBindings(false); EditDataChanedEvent?.Invoke(rhs); XtraMessageBox.Show("修改成功!"); }; dlg.ShowDialog(); } private void Add() { if (_SeriesID < 1) return; WaitFrmHelper.ShowWaitForm(); var dlg = new AddProductMotorDlg(); dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); }; dlg.SetBindingData(_SeriesID,_bindList?.Count() + 1 ?? 0); dlg.ReloadDataEvent += (rhs) => { if (_bindList == null) _bindList = new List(); if (rhs == null) return; _isChangeList = true; _bindList.Add(rhs); this.bindingSource1.ResetBindings(false); this.AddDataChanedEvent?.Invoke(rhs); this.FocusedDataChangedEvent?.Invoke(rhs); XtraMessageBox.Show("添加成功!"); }; dlg.ShowDialog(); } } }