tx
2025-04-09 fa7510e1ed63df0366787fa4ed1b3db6426d2b46
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.Drawing;
 
namespace TProduct.WinFrmUI.Data4Factory
{
    public partial class SelectPartBaseContentCtrl : DevExpress.XtraEditors.XtraUserControl
    {
        public SelectPartBaseContentCtrl()
        {
            InitializeComponent();
            // this.layoutControl1.SetupLayoutControl();
            // 
            this.gridViewMain.SetNormalView(30);
            this.gridViewMain.SetGridMianViewColor();
            this.gridViewMain.RegistCustomDrawRowIndicator();
            this.gridViewMain.OptionsView.EnableAppearanceOddRow = true;   // 使能 // 和和上面绑定 同时使用有效
            this.gridViewMain.Appearance.EvenRow.BackColor = Color.LightGray; // 设置偶数行颜色
            this.gridViewMain.OptionsView.EnableAppearanceEvenRow = true;
            this.gridViewMain.Appearance.OddRow.BackColor = Color.White; // 设置偶数行颜色
 
        }
        #region 当前视图 
        //数据拼接
        protected class CurrentViewModel : Model.PartBase
        {
            public CurrentViewModel() { }
            public CurrentViewModel(Model.PartBase obj) : base(obj)
            {
                this.ID = obj.ID;
                this.CreateUserID = obj.CreateUserID;
                this.CreateTime = obj.CreateTime;
                this.UpdateUserID = obj.UpdateUserID;
                this.UpdateTime = obj.UpdateTime;
                this.Code = obj.Code;
                this.Name = obj.Name;
            }
            public string ProductName { get; set; }
            public bool IsChecked { get; set; }
        }
 
        #endregion
 
        public event Action<Model.PartBase> FocusedDataChangedEvent = null;
        public event Action<Model.PartBase> AddDataChanedEvent = null;
        public event Action<Model.PartBase> EditDataChanedEvent = null;
 
        private List<CurrentViewModel> _bindList = null;
        private long _productID = 0;
        public void SetBindingData(long ProductID)
        {
            if (ProductID < 1)
                return;
            _productID = ProductID;
            _bindList = new List<CurrentViewModel>();
 
            var partList = new BLL.PartBase().GetByProductMainID(_productID);
            var product = new BLL.ProductMain().GetByID(_productID);
            partList?.ForEach(x => _bindList.Add(new CurrentViewModel(x) { ProductName = product?.Name }));
            this.bindingSource1.DataSource = _bindList;
            this.bindingSource1.ResetBindings(false);
            this.gridViewMain.FocusedRowHandle = 0;
        }
 
 
        /// <summary>
        /// 初始化并设置默认值
        /// </summary>
        public bool SetFocused(long ID)
        {
            this.SetBindingData(ID);
            var selModel = _bindList.Find(x => x.ID == ID);
            if (selModel == null)
                return false;
            if (selModel != null)
            {
                this.bindingSource1.ResetBindings(false);
                this.FocusedDataChangedEvent?.Invoke(selModel);
            }
            return true;
        }
        /// <summary>
        /// 获取聚焦
        /// </summary>
        public object GetFocused()
        {
            var vm = this.gridViewMain.GetFocusedRow() as CurrentViewModel;
            return vm;
        }
 
        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            var row = this.gridViewMain.GetFocusedRow() as CurrentViewModel;
            if (row == null)
                return;
            var model = row as Model.PartBase;
            this.FocusedDataChangedEvent?.Invoke(model);
        }
 
        private void barButAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Add(_productID);
        }
 
        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()
        {
            WaitFrmHelper.ShowWaitForm();
            var row = this.gridViewMain.GetCurrentViewModel(_bindList);
            if (row == null)
            {
                WaitFrmHelper.ShowWaitForm();
                XtraMessageBox.Show("请选择一行数据!");
                return;
            }
            var dlg = new EditPartBaseDlg();
            dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); };
            dlg.SetBindingData(row as Model.PartBase);
            dlg.ReloadDataEvent += (rhs) =>
            {
                row.Reset(rhs);
                this.bindingSource1.ResetBindings(false);
                EditDataChanedEvent?.Invoke(rhs as Model.PartBase);
                XtraMessageBox.Show("修改成功!");
            };
            dlg.ShowDialog();
        }
 
 
        private void Add(long ProductID)
        {
            if (ProductID < 1)
                return;
            WaitFrmHelper.ShowWaitForm();
            var dlg = new AddPartBaseOnListDlg();
            dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); };
            dlg.SetBindingData(ProductID);
            dlg.ReloadDataEvent += (rhs) =>
            {
                var ProductName = new BLL.ProductMain().GetByID(rhs.ProductMainID)?.Name;
                var vm = new CurrentViewModel(rhs) { ProductName = ProductName };
                _bindList.Add(vm);
                this.bindingSource1.ResetBindings(false);
                this.AddDataChanedEvent?.Invoke(rhs as Model.PartBase);
                this.FocusedDataChangedEvent?.Invoke(rhs as Model.PartBase);
                XtraMessageBox.Show("添加成功!");
            };
            dlg.ShowDialog();
        }
 
        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            var row = this.gridViewMain.GetFocusedRow() as CurrentViewModel;
            if (row == null)
                return;
            var model = row as Model.PartBase;
            this.FocusedDataChangedEvent?.Invoke(model);
        }
    }
}