using DevExpress.XtraEditors; using IStation.Model; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using static alglib; namespace IStation.WinFrmUI.Basic { public partial class AddElectricDlg : DevExpress.XtraEditors.XtraForm { public AddElectricDlg() { InitializeComponent(); } private BindingList _allBindingList = null; //这就是输入进去立马就获取到的值 public event Func ReloadDataEvent; public void SetBindingData() { _allBindingList = new BindingList(); 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 void btnOk_Click(object sender, EventArgs e) { if (!verify()) { return; } if (this.ReloadDataEvent == null) return; var Eleprice = new Model.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 = 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 Model.ElecPriceHourSetting; if (row == null) return; if (e.Column == this.ColDelete) _allBindingList.Remove(row); } } }