using DevExpress.XtraGrid.Views.Grid; using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Linq; using System.Windows.Forms; namespace TProduct.WinFrmUI.TValve { public partial class GridFeatMonitorRecordCtrl : UserControl { public GridFeatMonitorRecordCtrl() { InitializeComponent(); this.gridViewMain.OptionsDetail.ShowDetailTabs = false;//不显示TAB名 this.gridViewMain.OptionsView.ShowGroupPanel = false;//隐藏最上面的GroupPanel this.gridViewMain.OptionsSelection.MultiSelect = false;//单选 this.gridViewMain.OptionsBehavior.Editable = false;//只读 this.gridViewMain.BestFitColumns(); this.gridViewMain.IndicatorWidth = 40; this.gridViewMain.RowHeight = 35; this.gridViewMain.OptionsView.EnableAppearanceEvenRow = true; this.gridViewMain.OptionsView.EnableAppearanceOddRow = true; this.gridViewMain.Appearance.OddRow.BackColor = Color.AliceBlue; this.gridViewMain.Appearance.EvenRow.BackColor = Color.White; this.gridViewMain.RowClick += new DevExpress.XtraGrid.Views.Grid.RowClickEventHandler(this.GridViewMain_RowClick); this.gridViewMain.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(this.GridViewMain_CustomDrawRowIndicator); this.gridViewMain.CustomUnboundColumnData += new DevExpress.XtraGrid.Views.Base.CustomColumnDataEventHandler(this.GridViewMain_CustomUnboundColumnData); //this.gridViewMain.DoubleClick += new System.EventHandler(this.GridViewMain_DoubleClick); this.gridViewMain.RowCellClick += new DevExpress.XtraGrid.Views.Grid.RowCellClickEventHandler(this.GridViewMain_RowCellClick); flow_unit = TProduct.UserSetting.Setting.ValveTest.UnitFlow; press_unit = TProduct.UserSetting.Setting.ValveTest.UnitPress; } Eventech.Model.UnitP power_unit = Eventech.Model.UnitP.KW; Eventech.Model.UnitQ flow_unit = Eventech.Model.UnitQ.M3H; Eventech.Model.UnitH press_unit = Eventech.Model.UnitH.MPa; public Action OnEditRecord = null; public Action OnDeleteRecord = null; public Action OnAddRecordManu = null; private DataTable _recordTable = null; private List _allMonitorList = null; /// /// 设置 /// /// /// /// public bool SetBindingData(List allMonitorList) { if (allMonitorList == null || allMonitorList.Count() == 0) return false; this._allMonitorList = allMonitorList; _recordTable = new DataTable("RecordTable"); _recordTable.Columns.Add("ID", typeof(long)); _recordTable.Columns.Add("RecordTime", typeof(string)); _recordTable.Columns.Add("Degree", typeof(double)); _recordTable.Columns.Add("Density", typeof(double)); _recordTable.Columns.Add("Flow", typeof(double)); _recordTable.Columns.Add("Viscosity", typeof(double)); _recordTable.Columns.Add("P1", typeof(double)); _recordTable.Columns.Add("P2", typeof(double)); _recordTable.Columns.Add("P3", typeof(double)); _recordTable.Columns.Add("V", typeof(double)); _recordTable.Columns.Add("Re", typeof(double)); _recordTable.Columns.Add("DeltaP", typeof(double)); _recordTable.Columns.Add("Kv", typeof(double)); _recordTable.Columns.Add("Rc", typeof(double)); #region 设置GridView列 //DevExpress.XtraGrid.Views.BandedGrid.GridBand gridBandMonitor; //gridBandMonitor = new DevExpress.XtraGrid.Views.BandedGrid.GridBand(); //gridBandMonitor.AppearanceHeader.Options.UseTextOptions = true; //gridBandMonitor.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; //gridBandMonitor.Caption = "原始数据"; //gridBandMonitor.Name = "gridBandMmonitor"; //gridBandMonitor.VisibleIndex = 1; //gridBandMonitor.Width = 150; for (int i = this.gridBandMonitor.Columns.Count - 1; i >= 0; i--) { var col = this.gridBandMonitor.Columns[i]; this.gridBandMonitor.Columns.Remove(col); this.gridViewMain.Columns.Remove(col); } foreach (var p in allMonitorList) { DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn grid_col; grid_col = new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn(); grid_col.AppearanceHeader.Options.UseTextOptions = true; grid_col.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; grid_col.AppearanceCell.Options.UseTextOptions = true; grid_col.AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; if (p.MonitorType == Model.eMonitorType.功率) { grid_col.Caption = string.Format("{0}({1})", p.Name, Eventech.Common.UnitPHelper.GetEnUnitName(power_unit)); } else if (p.MonitorType == Model.eMonitorType.流量) { grid_col.Caption = string.Format("{0}({1})", p.Name, Eventech.Common.UnitQHelper.GetEnUnitName(flow_unit)); } else if (p.MonitorType == Model.eMonitorType.压力) { grid_col.Caption = string.Format("{0}({1})", p.Name, Eventech.Common.UnitHHelper.GetEnUnitName(press_unit)); } else { grid_col.Caption = p.Name; } grid_col.Name = "bandedGridColumn" + p.ID; grid_col.Visible = true; grid_col.FieldName = string.Format("M{0}", p.ID); gridBandMonitor.Columns.Add(grid_col); this.gridViewMain.Columns.Add(grid_col); _recordTable.Columns.Add(string.Format("M{0}", p.ID), typeof(string)); } #endregion this.bindingSource1.DataSource = _recordTable; return true; } // 清空数据 public bool ClearData() { _recordTable.Clear(); this.bindingSource1.DataSource = _recordTable; this.bindingSource1.ResetBindings(false); return true; } public void ExportExcel() { SaveFileDialog dlg = new SaveFileDialog(); dlg.Title = "记录数据列表"; dlg.FileName = "记录数据列表.xls"; dlg.Filter = "excel文件 (*.xls)|*.xls"; if (dlg.ShowDialog() == DialogResult.OK) this.gridControl1.ExportToXls(dlg.FileName); } List _allRecords = null; //刷新数据源 public bool RefreshData(List allRecords) { this._allRecords = allRecords; _recordTable.Clear(); if (allRecords == null || allRecords.Count == 0) { return true; } var rowTemplate = _recordTable.NewRow(); foreach (TProduct.Model.ValveFeatTestRecordViewModel record in allRecords) { var rowGrid = _recordTable.NewRow(); rowGrid.ItemArray = rowTemplate.ItemArray; rowGrid["ID"] = record.ID; rowGrid["RecordTime"] = record.Time.ToString("yyyy-MM-dd HH:mm:ss"); rowGrid["Degree"] = record.Degree; rowGrid["Density"] = record.Density; rowGrid["Viscosity"] = record.Viscosity; rowGrid["Flow"] = Eventech.Common.UnitQHelper.fromM3H(flow_unit, record.Flow); rowGrid["P1"] = Eventech.Common.UnitHHelper.fromMPa(press_unit, record.P1); rowGrid["P2"] = Eventech.Common.UnitHHelper.fromMPa(press_unit, record.P2); rowGrid["P3"] = Eventech.Common.UnitHHelper.fromMPa(press_unit, record.P3); rowGrid["DeltaP"] = Eventech.Common.UnitHHelper.fromMPa(press_unit, record.DeltaP); rowGrid["V"] = record.V; rowGrid["Re"] = record.Re; rowGrid["Kv"] = record.Kv; rowGrid["Rc"] = record.Rc; foreach (var p in this._allMonitorList) { var col = string.Format("M{0}", p.ID); var r = record.MonitorRecordList.Find(x => x.ID == p.ID); if (r != null) { if (string.IsNullOrEmpty(r.Value)) { rowGrid[col] = ""; continue; } if (p.MonitorType == Model.eMonitorType.功率) { rowGrid[col] = Eventech.Common.UnitPHelper.fromKW( power_unit, Convert.ToDouble(r.Value)).ToString(); } else if (p.MonitorType == Model.eMonitorType.流量) { rowGrid[col] = Eventech.Common.UnitQHelper.fromM3H( flow_unit, Convert.ToDouble(r.Value)).ToString(); } else if (p.MonitorType == Model.eMonitorType.压力) { rowGrid[col] = Eventech.Common.UnitHHelper.fromMPa( press_unit, Convert.ToDouble(r.Value)).ToString(); } else { rowGrid[col] = r.Value; } } } _recordTable.Rows.Add(rowGrid); } this.bindingSource1.DataSource = _recordTable; this.bindingSource1.ResetBindings(false); return true; } /// /// /// /// public long GetFocusRowRecordID() { var row = (this.gridViewMain.GetRow(gridViewMain.GetFocusedDataSourceRowIndex()) as System.Data.DataRowView).Row; if (row == null) return 0; return Convert.ToInt64(row["ID"]); } #region GridViewMain // TProduct.Model.ValveFeatTestRecordViewModel _clickProjectRow = null; private void GridViewMain_RowClick(object sender, RowClickEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Right) { // _clickProjectRow = gridViewMain.GetRow(e.RowHandle) as TProduct.Model.ValveFeatTestRecordViewModel; //_rightClickProject = (gridViewMain.GetRow(e.RowHandle) as System.Data.DataRowView).Row as TProduct.WinFrmUI.Sale.DataSetUI.SaleProjectRow; // _rightClickProject = gridViewMain.GetFocusedDataRow() as TProduct.WinFrmUI.Sale.DataSetUI.SaleProjectRow; //if (_clickProjectRow != null) //{ // if (_clickProjectRow.ProjectStyle == 1) // { //简易项目 // MenuItem转为简易项目.Visible = false; // } // else // { // MenuItem转为简易项目.Visible = true ; // } // GidViewMenuStrip.Show(gridControl1, e.Location); //} } } // private void GridViewMain_DoubleClick(object sender, EventArgs e) { //var view = sender as GridView; //if (view != null) //{ // var point = view.GridControl.PointToClient(MousePosition); // var pumpStr = view.CalcHitInfo(point); // if (pumpStr.InRow || pumpStr.InRowCell) // { // // _clickPrjItem = gridViewAttach.GetRow(pumpStr.RowHandle) as MotorInfoExCatalogName; // //// _clickPrjItem = (gridViewAttach.GetRow(pumpStr.RowHandle) as System.Data.DataRowView).Row as DAL.DataSetCustomer.CustomerVipInfoRow; // // if (_clickPrjItem != null) // // { // // if (_clickPrjItem.RecordGID == Guid.Empty) // // return; // // } // } //} } private void GridViewMain_RowCellClick(object sender, RowCellClickEventArgs e) { //if (e.Column == colDelete) //{ // var prjEntity = // this.gridViewMain.GetRow(e.RowHandle) as TProduct.Model.ValveFeatTestRecordViewModel; // OnDeleteRecord(prjEntity); //} //if (e.Column == colEdit) //{ // var prjEntity = // this.gridViewMain.GetRow(e.RowHandle) as TProduct.Model.ValveFeatTestRecordViewModel; // OnEditRecord(prjEntity); //} } private void GridViewMain_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e) { if (e.Info.IsRowIndicator && e.RowHandle >= 0) { e.Info.DisplayText = (e.RowHandle + 1).ToString(); } } private void GridViewMain_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) { //if (e.Column == colCreateTime) //{ // var prj = e.Row as TProduct.Model.SaleProject; // if (prj == null) // return; // e.Value = prj.GetCreateTime(); //} } #endregion private void bbi手动输入记录_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (this.OnAddRecordManu != null) { this.OnAddRecordManu.Invoke(); } } private void bbiEditRecord_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var row = gridViewMain.GetFocusedDataRow(); if (row == null) return; var record_id = Convert.ToInt64(row["ID"]); var record = this._allRecords.Find(x => x.ID == record_id); if (record != null && OnEditRecord != null) { OnEditRecord(record); } } private void bbiDeleteRecord_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var row = gridViewMain.GetFocusedDataRow(); if (row == null) return; var record_id = Convert.ToInt64(row["ID"]); var record = this._allRecords.Find(x => x.ID == record_id); if (record != null && OnDeleteRecord != null) { OnDeleteRecord(record); } } private void bbiExportExcel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { ExportExcel(); } public Action OnEmptyAllRecord = null; private void bbi清空所有数据_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (OnEmptyAllRecord == null) return; OnEmptyAllRecord.Invoke(); } } }