ningshuxia
2025-03-31 d321346f204a69b1929cc39098a5d88f44509e7b
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
using DevExpress.XtraEditors;
using System;
using System.ComponentModel;
using System.Linq;
 
namespace IStation.WinFrmUI.Monitor
{
    /// <summary>
    /// 比较映射
    /// </summary>
    public partial class CompareMappingMatrixDlg : XtraForm
    {
        public CompareMappingMatrixDlg()
        {
            InitializeComponent();
 
            this.repImgCmbCompare.BeginUpdate();
            this.repImgCmbCompare.Items.Clear();
            this.repImgCmbCompare.Items.AddEnum(typeof(Model.eCompareType), false);
            this.repImgCmbCompare.EndUpdate();
        }
 
 
        public event Func<string, bool> ReloadDataEvent;
        private BindingList<Model.CompareMappingMatrix> _bindingList = null;
 
        /// <summary>
        /// 绑定数据
        /// </summary> 
        public void Set(string paras, Model.SignalType signalType)
        {
            var list = Model.CompareMappingMatrix.ToList(paras);
            if (list == null)
                list = new System.Collections.Generic.List<Model.CompareMappingMatrix>();
            _bindingList = new BindingList<Model.CompareMappingMatrix>(list);
 
            if (signalType != null)
            {
                var formatParas = signalType.GetEnumFormatParas();
                if (formatParas != null && formatParas.Items != null)
                {
                    this.repImgCmbValue.BeginUpdate();
                    this.repImgCmbValue.Items.Clear();
                    foreach (var item in formatParas.Items)
                    {
                        this.repImgCmbValue.Items.Add(item.Name, (double)item.Value, -1);
                    }
                    this.repImgCmbValue.EndUpdate();
                }
            }
 
            this.bindingSource1.DataSource = _bindingList;
            this.bindingSource1.ResetBindings(false);
        }
 
        //验证
        public bool Valid()
        {
            this.gridView1.CloseEditor();
            this.gridView1.UpdateCurrentRow();
            if (_bindingList.Count > 0)
            {
                for (int i = 0; i < _bindingList.Count; i++)
                {
                    var vm = _bindingList[i];
                    if (string.IsNullOrEmpty(vm.Value.ToString()))
                    {
                        this.gridView1.FocusedRowHandle = i;
                        this.gridView1.SetColumnError(colValue, "必填项");
                        //XtraMessageBox.Show("请将值映射信息设置完整!");
                        return false;
                    }
                }
            }
            return true;
        }
 
        #region GridView 
 
        //删除
        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (_bindingList == null || _bindingList.Count < 1)
                return;
            var row = this.gridView1.GetFocusedRow() as Model.CompareMappingMatrix;
            if (row == null)
                return;
            if (e.Column == this.colDelete)
                _bindingList.Remove(row);
        }
 
        //新增属性时必须先输入属性名 
        private void gridView1_ShowingEditor(object sender, CancelEventArgs e)
        {
            if (_bindingList == null)
            {
                e.Cancel = true;
                return;
            }
            if (this.gridView1.FocusedColumn != this.colKey)
            {
                var row = this.gridView1.GetFocusedRow() as Model.CompareMappingMatrix;
                if (row == null)
                {
                    e.Cancel = true;
                    return;
                }
            }
        }
        #endregion
 
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!Valid())
                return;
 
            string json = string.Empty;
            if (_bindingList.Any())
            {
                json = Model.CompareMappingMatrix.ToJson(_bindingList.ToList());
            }
 
            if (this.ReloadDataEvent != null)
            {
                var bol = this.ReloadDataEvent(json);
                if (!bol)
                {
                    XtraMessageBox.Show("设置失败!");
                    return;
                }
                XtraMessageBox.Show("设置成功!");
            }
 
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
 
        }
    }
}