// 计算当前数值的状态信息 private CExDataGridView.EDataState GetDataStatus(CEntityRealTime entity) { // 看是否设定最大值,以及最小值,还有变化值之类的东东啦 // CExDataGridView.EDataState state = CExDataGridView.EDataState.ENormal; // TimeSpan span = entity.TimeReceived - entity.TimeDeviceGained; // int offset = Math.Abs(span.Minutes); // if (offset > 5 && offset < 10) // { // state = CExDataGridView.EDataState.EWarning; // } // else if (offset > 10) // { // state = CExDataGridView.EDataState.EError; // } // return state; CExDataGridView.EDataState state = GetDataStatus(entity.TimeDeviceGained); if (state == EDataState.ENormal) { // 只有没超时的时候,才会显示实时数据的状态 switch (entity.ERTDState) { case ERTDDataState.EError: state = CExDataGridView.EDataState.EPink; break; case ERTDDataState.ENormal: state = CExDataGridView.EDataState.ENormal; break; case ERTDDataState.EWarning: state = CExDataGridView.EDataState.EWarning; break; } } return(state); }
protected virtual void ExCellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (!m_mutexDataTable.WaitOne(0)) { // 表示当前正在后台更新数据,暂时不更新界面消息 return; } //m_mutex.WaitOne(); if (e.RowIndex < 0) { return; } try { //Debug.WriteLine("cell format...{0}-{1}",e.RowIndex,e.ColumnIndex); DataGridViewRow CurrentRow = this.Rows[e.RowIndex]; CurrentRow.HeaderCell.Value = Convert.ToString(e.RowIndex + 1 + (m_iCurrentPage - 1) * m_iPageRowCount);//显示行号,也可以设置成显示其他信息 // 此时保证枚举前面没有 手动改变枚举的值 CExDataGridView.EDataState state = (CExDataGridView.EDataState)Int32.Parse(CurrentRow.Cells[m_arrayStrHeader.Length].Value.ToString()); switch (state) { case EDataState.ENormal: { if (e.RowIndex % 2 == 1) { // 浅绿 this.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.FromArgb(248, 255, 255); } } break; case EDataState.EWarning: { // 警告 this.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Yellow; } break; case EDataState.EError: { // 错误 this.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Red; //this.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.Font = new System.Drawing.Font("Arial", 10); } break; case EDataState.EDeleted: { // 被标记为删除 this.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Gray; //this.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.Font = new System.Drawing.Font("Arial", 10); } break; case EDataState.EPink: { // 实时数据中标记为越界显示 this.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.FromArgb(255, 128, 255); } break; }//switch // 释放互斥量,表示后台可以更新数据了 } catch (Exception ex) { Debug.WriteLine(ex.ToString()); } finally { m_mutexDataTable.ReleaseMutex(); } }