Shuxia Ning
2024-10-08 cf4967a0aebab18c5a37137f3e4c61b2d73a54bb
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
using DevExpress.Utils.Extensions;
using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
 
namespace IStation.WinFrmUI.Monitor
{
    /// <summary>
    /// 设置校验点  
    /// </summary>
    public partial class SetCheckPointsDlg : XtraForm
    {
        public SetCheckPointsDlg()
        {
            InitializeComponent();
        }
 
        public event Func<List<Model.CurveAnalyzePoint>, bool> ReloadDataEvent;
        private BindingList<Model.CurveAnalyzePoint> _bindingList = null;
 
        /// <summary>
        /// 绑定数据
        /// </summary> 
        public void Set(List<Model.CurveAnalyzePoint> checkPoints)
        {
            if (checkPoints != null && checkPoints.Any())
            {
                _bindingList = new BindingList<Model.CurveAnalyzePoint>(checkPoints);
            }
            else
            {
                _bindingList = new BindingList<Model.CurveAnalyzePoint>();
            }
 
 
            this.curveAnalyzePointBindingSource.DataSource = _bindingList;
            this.curveAnalyzePointBindingSource.ResetBindings(false);
            this.gridView1.BestFitColumns();
        }
 
        //验证
        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.HZ.ToString()))
                    {
                        this.gridView1.FocusedRowHandle = i;
                        this.gridView1.SetColumnError(this.colHz, "必填项");
                        return false;
                    }
 
                    if (string.IsNullOrEmpty(vm.Q.ToString()))
                    {
                        this.gridView1.FocusedRowHandle = i;
                        this.gridView1.SetColumnError(this.colHz, "必填项");
                        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;
            if (e.Column == this.colDelete)
            {
                var row = this.gridView1.GetFocusedRow() as Model.CurveAnalyzePoint;
                if (row == null)
                    return;
                _bindingList.Remove(row);
            }
        }
 
        //新增属性时必须先输入属性名 
        private void gridView1_ShowingEditor(object sender, CancelEventArgs e)
        {
            if (_bindingList == null)
            {
                e.Cancel = true;
                return;
            }
            if (this.gridView1.FocusedColumn != this.colHz)
            {
                var row = this.gridView1.GetFocusedRow() as Model.CurveAnalyzePoint;
                if (row == null)
                {
                    e.Cancel = true;
                    return;
                }
            }
        }
 
        //验证名称的唯一性
        private void gridView1_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e)
        {
            if (_bindingList == null || _bindingList.Count < 1)
                return;
            if (this.gridView1.FocusedColumn == this.colHz)
            {
                if (double.TryParse(e.Value.ToString(), out double value))
                {
                    if (value <= 0)
                    {
                        e.ErrorText = "小于或等于0";
                        e.Valid = false;
                    }
                    else if (value > 50)
                    {
                        e.ErrorText = "大于50";
                        e.Valid = false;
                    }
                    var row = this.gridView1.GetFocusedRow() as Model.CurveAnalyzePoint;
                    var list = _bindingList.ToList();
                    list.Remove(row);
                    if (list.Find(x => x.HZ == Convert.ToDouble(e.Value)) != null)
                    {
                        e.ErrorText = "重复";
                        e.Valid = false;
                    }
                }
            }
        }
 
        #endregion
 
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!Valid())
                return;
 
            var list = _bindingList?.ToList();
            if (this.ReloadDataEvent != null)
            {
                var bol = this.ReloadDataEvent(list);
                if (!bol)
                {
                    XtraMessageBox.Show("设置失败!");
                    return;
                }
                XtraMessageBox.Show("设置成功!");
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
 
        }
 
    }
}