using DevExpress.XtraEditors; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Windows.Forms; namespace TProduct.WinFrmUI.Data4Factory { public partial class MgrPartBaseListInProductCtrl : DevExpress.XtraEditors.XtraUserControl { public MgrPartBaseListInProductCtrl() { InitializeComponent(); this.layoutControl1.SetupLayoutControl(); this.gridView1.SetNormalEditView(); this.gridView1.SetGridMianViewColor(); } protected class CurrentViewModel : Model.PartBase { public CurrentViewModel() { } public CurrentViewModel(CurrentViewModel rhs) : base(rhs) { this.MfName = rhs.MfName; this.SdName = rhs.SdName; this.LastTestUserName = rhs.LastTestUserName; } public CurrentViewModel(Model.PartBase rhs, string MfName, string SdName, string LastTestUserName) : base(rhs) { this.MfName = MfName; this.SdName = SdName; this.LastTestUserName = LastTestUserName; } public string MfName { get; set; } public string LastTestUserName { get; set; } public string SdName { get; set; } } public event Action FocusedDataChangedEvent = null; public event Action AddDataChanedEvent = null; public event Action EditDataChanedEvent = null; private List _bindList = null; private List _allUserList = null; private List _allManufacturerList = null; private List _allSenderList = null; private long _currentProductID = 0; public void SetBindingData(long ProductID) { this.colMfName.Visible = TProduct.UserSetting.Setting.Disp.IsShowManufacturer; this.colSdName.Visible = TProduct.UserSetting.Setting.Disp.IsShowSender; this._currentProductID = ProductID; _allManufacturerList = new BLL.ManufacturerBase().GetAll(); _allSenderList = new BLL.Senderbase().GetAll(); _allUserList = new BLL.LoginUser().GetAll(); _bindList = new List(); if (ProductID > 0) { var parts = new BLL.PartBase().GetByProductMainID(ProductID); if (parts != null && parts.Count > 0) { foreach (var part in parts) { _bindList.Add( new CurrentViewModel(part, (from x in _allManufacturerList where x.ID == part.ManufacturerID select x.FullName.Length < 1 ? x.ShortName : x.FullName).FirstOrDefault(), (from x in _allSenderList where x.ID == part.SenderID select x.FullName.Length < 1 ? x.ShortName : x.FullName).FirstOrDefault(), _allUserList.Find(x => x.ID == part.LastTestUserID)?.RealName)); } } } this.bindingSource1.DataSource = _bindList; this.bindingSource1.ResetBindings(false); } public bool IsUpdate; public List GetAllPartList() { var partList = new List(); if (_bindList != null && _bindList.Count > 0) { _bindList.ForEach(x => partList.Add(x)); } return partList; } 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.gridView1.OptionsFind.AlwaysVisible = this.barCkSelect.Checked; } private void Add() { var dlg = new AddPartBaseOnListDlg(); dlg.SetBindingData(this._currentProductID); dlg.ReloadDataEvent += (part) => { var vm = new CurrentViewModel(part, (from x in _allManufacturerList where x.ID == part.ManufacturerID select x.FullName.Length < 1 ? x.ShortName : x.FullName).FirstOrDefault(), (from x in _allSenderList where x.ID == part.SenderID select x.FullName.Length < 1 ? x.ShortName : x.FullName).FirstOrDefault(), _allUserList.Find(u => u.ID == part.LastTestUserID)?.RealName); if (_bindList == null) _bindList = new List(); _bindList.Add(vm); this.bindingSource1.ResetBindings(false); this.gridView1.RefreshData(); this.AddDataChanedEvent?.Invoke(part); this.FocusedDataChangedEvent?.Invoke(part); XtraMessageBox.Show("添加成功!"); }; dlg.ShowDialog(); } private void Edit() { WaitFrmHelper.ShowWaitForm(); CurrentViewModel row = this.gridView1.GetCurrentViewModel(_bindList); if (row == null) { WaitFrmHelper.ShowWaitForm(); XtraMessageBox.Show("请选择一行数据!"); return; } var dlg = new EditPartBaseDlg(); dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); }; dlg.SetBindingData(row); dlg.ReloadDataEvent += (rhs) => { if (rhs.ManufacturerID > 0) row.MfName = new BLL.ManufacturerBase().GetByID(rhs.ManufacturerID)?.ShortName; if (rhs.SenderID > 0) row.SdName = new BLL.Senderbase().GetByID(rhs.SenderID)?.ShortName; row.Reset(rhs); this.bindingSource1.ResetBindings(false); this.gridView1.RefreshData(); EditDataChanedEvent?.Invoke(rhs); XtraMessageBox.Show("修改成功!"); IsUpdate = true; }; dlg.ShowDialog(); } private void barBtnDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (_bindList == null || _bindList.Count < 1) return; var row = this.gridView1.GetCurrentViewModel(_bindList); if (row == null) { XtraMessageBox.Show("请选择一行数据!"); return; } DialogResult dr = XtraMessageBox.Show("您确定要'" + row.Name + "'这条数据吗?", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (dr == DialogResult.OK) { if (row.ID > 0) { var del = new BLL.PartBase().DeleteByID(row.ID); if (del) { _bindList.RemoveAt(_bindList.LastIndexOf(row)); XtraMessageBox.Show("删除成功!"); } } else { _bindList.RemoveAt(_bindList.LastIndexOf(row)); XtraMessageBox.Show("删除成功!"); } } } private void bbiBtnCopy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var row = this.gridView1.GetCurrentViewModel(_bindList); if (row == null) { XtraMessageBox.Show("请选择一行数据!"); return; } WaitFrmHelper.ShowWaitForm(); var copy = new CurrentViewModel(row); copy.Code = new BLL.PartBase().CreateNO(); copy.LastTestUserID = 0; copy.CreateUserID = TProduct.WinFrmUI.GlobeParas.CurrentLoginUser.ID; copy.CreateTime = DateTime.Now; copy.ID = 0; _bindList.Add(copy); this.bindingSource1.ResetBindings(false); var dlg = new EditPartBaseDlg(); dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); }; dlg.SetBindingData(copy); dlg.ReloadDataEvent += (rhs) => { copy.Reset(rhs); if (copy.ManufacturerID > 0) row.MfName = new BLL.ManufacturerBase().GetByID(rhs.ManufacturerID)?.ShortName; if (copy.SenderID > 0) row.SdName = new BLL.Senderbase().GetByID(rhs.SenderID)?.ShortName; this.bindingSource1.ResetBindings(false); AddDataChanedEvent?.Invoke(rhs); }; dlg.ShowDialog(); } } }