ningshuxia
2025-03-27 0b0f37c86f484b10d911b56b77968f832ce69bbb
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
using DevExpress.XtraEditors;
using System.ComponentModel;
using System.Data;
 
namespace PBS.WinFrmUI
{
    public partial class AddElectricDlg : DevExpress.XtraEditors.XtraForm
    {
        public AddElectricDlg()
        {
            InitializeComponent();
        }
 
        private BindingList<Vmo.ElecPriceHourSetting> _allBindingList = null; //这就是输入进去立马就获取到的值
 
        public event Func<Vmo.ElecPriceMonthSetting, Task<bool>> ReloadDataEvent;
 
        public void SetBindingData()
        {
            _allBindingList = new BindingList<Vmo.ElecPriceHourSetting>();
            this.bindingSource1.DataSource = _allBindingList;
        }
 
        //验证
        private bool verify()
        {
            this.dxErrorProvider1.ClearErrors();
            if (string.IsNullOrEmpty(this.txtStartMonth.Text))
            {
                this.dxErrorProvider1.SetError(this.txtStartMonth, "必填项");
                return false;
            }
            if (string.IsNullOrEmpty(this.txtEndMonth.Text))
            {
                this.dxErrorProvider1.SetError(this.txtEndMonth, "必填项");
                return false;
            }
            var AllHour = _allBindingList;
            var hour = AllHour.OrderBy(x => x.EndHour).ToList();
            var HourCount = hour.Count;
            if (hour.First().StartHour != 0 || hour.Last().EndHour != 24)
            {
                XtraMessageBox.Show("开始时间和结束时间有误");
                return false;
            }
            for (int x = 0; x < hour.Count - 1; x++)
            {
                var frist = hour[x];
                var second = hour[x + 1];
                var last = hour[hour.Count - 1];
                if (second.StartHour == second.EndHour)
                {
                    XtraMessageBox.Show("开始时间和结束时间相同");
                    return false;
                }
                if (frist.EndHour != second.StartHour)
                {
                    XtraMessageBox.Show("时间输入有误");
                    return false;
                }
            }
            return true;
        }
 
        //确定
        private async void btnOk_Click(object sender, EventArgs e)
        {
            if (!verify())
            {
                return;
            }
            if (this.ReloadDataEvent == null)
                return;
            var Eleprice = new Vmo.ElecPriceMonthSetting();
            int Startmonth = 0;
            if (!int.TryParse(this.txtStartMonth.Text, out Startmonth))
            {
                MessageBox.Show("输入有误");
                return;
            }
            /*            if (Startmonth > 11)
                        {
                            MessageBox.Show("开始月小于12");
                            return;
                        }
                        */
            Eleprice.StartMonth = Startmonth;
            int Endmonth = 0;
            if (!int.TryParse(this.txtEndMonth.Text, out Endmonth))
            {
                MessageBox.Show("输入有误");
                return;
            }
 
            if (Endmonth < 1 || Endmonth > 12)
            {
                MessageBox.Show("请输入不小于1且不大于12的数字");
                return;
            }
            /*   if (Startmonth >=Endmonth)
               {
                   MessageBox.Show("开始月不能小于或等于结束月");
                   return;
               }*/
            Eleprice.EndMonth = Endmonth;
            Eleprice.HourList = _allBindingList.ToList();        //把绑定列表的值赋给hourlist
            var isok = await this.ReloadDataEvent.Invoke(Eleprice);
            if (isok == false)
            {
                XtraMessageBox.Show("添加失败");
                return;
            }
            XtraMessageBox.Show("添加成功");
            this.DialogResult = System.Windows.Forms.DialogResult.OK;       //确定ok,然后关闭
            this.Close();
        }
 
        //行删除
        private void gridView2_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (_allBindingList == null || _allBindingList.Count < 1)
                return;
            var row = this.gridView2.GetFocusedRow() as Vmo.ElecPriceHourSetting;
            if (row == null)
                return;
            if (e.Column == this.ColDelete)
                _allBindingList.Remove(row);
        }
    }
}