using DevExpress.XtraEditors; using IStation.Untity; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace IStation.WinFrmUI.Basic { public partial class SignalTypeMgrPage : DocumentPage { public SignalTypeMgrPage() { InitializeComponent(); this.PageTitle.Caption = "信号类型"; this.gridView1.SetNormalView(); //this.gridView1.SetGridMianViewColor(); this.gridView1.RegistCustomDrawRowIndicator(); this.gridView1.OptionsView.ShowViewCaption = false; this.gridView1.OptionsView.ShowDetailButtons = true; } public class CurrentViewModel : Model.SignalType { public CurrentViewModel() { } public CurrentViewModel(Model.SignalType rhs) : base(rhs) { this.MeasureTypeDisplay = rhs.MeasureType.GetDisplayText(); this.FormatTypeDisplay = rhs.FormatType.GetDisplayText(); this.FormatParasDisplay = SignalTypeHelper.ToString(rhs); } public string MeasureTypeDisplay { get; set; } public string FormatTypeDisplay { get; set; } public string FormatParasDisplay { get; set; } public new void Reset(Model.SignalType rhs) { base.Reset(rhs); this.MeasureTypeDisplay = rhs.MeasureType.GetDisplayText(); this.FormatTypeDisplay = rhs.FormatType.GetDisplayText(); this.FormatParasDisplay = SignalTypeHelper.ToString(rhs); } } private BLL.SignalType _bll = new(); private List _allBindingList = null;//所有列表 private Model.SignalTypeGroup _group = null; /// /// 初始化数据 /// public override void InitialDataSource() { SignalTypeHelper.Initial(); this.signalTypeGroupMgrCtrl1.FocusedChangedEvent += SignalTypeGroupMgrCtrl1_FocusedChangedEvent; this.signalTypeGroupMgrCtrl1.SetBindingData(); _allBindingList = new List(); this.signalTypeBindingSource.DataSource = _allBindingList; this.signalTypeBindingSource.ResetBindings(false); } private void SignalTypeGroupMgrCtrl1_FocusedChangedEvent(Model.SignalTypeGroup arg) { _group = arg; SetBindingData(arg); } public async void SetBindingData(Model.SignalTypeGroup model) { _allBindingList.Clear(); if (model != null) { WaitFrmHelper.ShowWaitForm("正在加载数据..."); var list = await _bll.GetByGroupID(model.ID); if (list != null && list.Count > 0) { foreach (var item in list) { var vm = new CurrentViewModel(item); _allBindingList.Add(vm); } } this.gridView1.BestFitColumns(); WaitFrmHelper.HideWaitForm(); } this.signalTypeBindingSource.ResetBindings(false); } /// /// 刷新数据 /// public override void RefreshDataSource() { SetBindingData(_group); } //添加 private void barBtnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (_group == null) return; if (_allBindingList == null) return; var sortCode = _allBindingList.Count < 1 ? 1 : _allBindingList.Max(x => x.SortCode) + 1; WaitFrmHelper.ShowWaitForm(); var dlg = new AddSignalTypeDlg(); dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); }; dlg.SetBindingData(sortCode); dlg.VerifyIdentifierExistEvent += async (identifier) => { return await _bll.IsExistByIdentifier(identifier); }; dlg.ReloadDataEvent += async (rhs) => { rhs.GroupID = _group.ID; var id = await _bll.Insert(rhs); if (id > 0) { var model = await _bll.GetByID(id); var vm = new CurrentViewModel(model); _allBindingList.Add(vm); this.signalTypeBindingSource.DataSource = _allBindingList; this.signalTypeBindingSource.ResetBindings(false); return true; } return false; }; dlg.ShowDialog(); } //编辑 private void barBtnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var vm = this.gridView1.GetCurrentViewModel(_allBindingList); if (vm == null) { XtraMessageBox.Show("请选择数据行!"); return; } WaitFrmHelper.ShowWaitForm(); var dlg = new EditSignalTypeDlg(); dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); }; dlg.SetBindingData(vm); dlg.VerifyIdentifierExistEvent += async (identifier, id) => { return await _bll.IsExistIdentifierExceptID(identifier, id); }; dlg.ReloadDataEvent += async (rhs) => { var bol = await _bll.Update(rhs); if (bol) { var model = await _bll.GetByID(rhs.ID); vm.Reset(model); this.gridView1.RefreshRow(this.gridView1.FocusedRowHandle); } return bol; }; dlg.ShowDialog(); } //删除 private async void barBtnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var vm = this.gridView1.GetCurrentViewModel(_allBindingList); if (vm == null) return; if (XtraMessageBox.Show($"确认删除数据行?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) != DialogResult.OK) return; var output = await new BLL.SignalType().DeleteByID(vm.ID); var result = output?.Success ?? false; if (!result) { XtraMessageBox.Show($"删除失败!\n原因:{output.Reason}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (!string.IsNullOrEmpty(output.Reason)) XtraMessageBox.Show($"删除成功!\n警告:{output.Reason}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); else XtraMessageBox.Show($"删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); _allBindingList.Remove(vm); this.signalTypeBindingSource.ResetBindings(false); } //详情 private void barBtnDetails_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { XtraMessageBox.Show("待补充!"); } //排序码 private void barBtnSortCode_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var vm = this.gridView1.GetCurrentViewModel(_allBindingList); if (vm == null) { XtraMessageBox.Show("请选择数据行!"); return; } var dlg = new SetSortCodeDlg(); dlg.SetBindingData(vm.SortCode); dlg.ReloadDataEvent += async (rhs) => { var result = await _bll.UpdateSortCode(vm.ID, rhs); if (result) { var model = await _bll.GetByID(vm.ID); vm.Reset(model); this.gridView1.RefreshRow(this.gridView1.FocusedRowHandle); } return result; }; dlg.ShowDialog(); } // 刷新 private void barBtnReload_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { RefreshDataSource(); } // 导出Excel private void barBtnExcelDown_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { this.gridView1.ExportExcel(this, _pageOperateInfo); } } }