From 9e18436f4e548303b668cabd65474245c34c1c4e Mon Sep 17 00:00:00 2001 From: tdx Date: Wed, 2 Apr 2025 09:33:03 +0000 Subject: [PATCH] SVN r436 SVN-Revision: r436 --- 插件库/Lskj.Control/BandedGridControlEx.cs | 75 ++++++++++++++++++---- 1 file changed, 63 insertions(+), 12 deletions(-) diff --git a/插件库/Lskj.Control/BandedGridControlEx.cs b/插件库/Lskj.Control/BandedGridControlEx.cs index a3c7d62..b481f07 100644 --- a/插件库/Lskj.Control/BandedGridControlEx.cs +++ b/插件库/Lskj.Control/BandedGridControlEx.cs @@ -263,18 +263,32 @@ namespace Lskj.Control /// public void OnGridViewRowCellStyle(object sender, RowCellStyleEventArgs e) { - if (e.RowHandle >= 0 && this.GridRowColorsTable != null && this.GridRowColorsTable.Rows.Count > 0 ) + var view = sender as DevExpress.XtraGrid.Views.Grid.GridView; + + if (view.IsCellSelected(e.RowHandle, e.Column)) { - // 渲染界面数据量过多界面刷新会很慢 - //DataTable table = this.gridControl.DataSourceTable(); - //DataRow gridRow = table.Rows[e.RowHandle]; - DataRow gridRow = this.GridView.GetDataRow(e.RowHandle); + return; // 如果当前单元格被选中,则不应用自定义样式 + } + + if (e.RowHandle >= 0 && this.GridRowColorsTable != null && this.GridRowColorsTable.Rows.Count > 0) + { + DataRow gridRow = view.GetDataRow(e.RowHandle); + // 获取行的最大日期值 + DateTime? maxDateForRow = GetMaxDateFromColumnNames(gridRow); foreach (DataRow item in this.GridRowColorsTable.Rows) { string cond = ReplaceHelper.ReplaceRowParam(gridRow, item["condition"] + "").ReplaceColumnParam(e.Column.Name, e.Column.Caption, e.CellValue + ""); + bool isLaterDatePresent = false; + // 检查当前列是否为日期类型并获取日期 + DateTime currentDate; + if (IsDateColumn(e.Column.FieldName, out currentDate) && maxDateForRow.HasValue && currentDate < maxDateForRow.Value) + { + // 如果当前日期小于行的最大日期,则跳过当前迭代 + isLaterDatePresent = true; + } try { - if (!string.IsNullOrWhiteSpace(cond) && ReplaceHelper.EvalCond(cond)) + if (!string.IsNullOrWhiteSpace(cond) && ReplaceHelper.EvalCond(cond) && (!string.IsNullOrEmpty(e.CellValue + "")|| isLaterDatePresent)) { GridRowColorModel model = new GridRowColorModel(item); if (!string.IsNullOrWhiteSpace(model.BackColor)) @@ -287,21 +301,58 @@ namespace Lskj.Control } FontStyle fontStyle = new FontStyle(); - if (model.IsBold) fontStyle = fontStyle | FontStyle.Bold; - if (model.IsItalic) fontStyle = fontStyle | FontStyle.Italic; - if (model.IsStrickOut) fontStyle = fontStyle | FontStyle.Strikeout; - if (model.IsUnderLine) fontStyle = fontStyle | FontStyle.Underline; + if (model.IsBold) fontStyle |= FontStyle.Bold; + if (model.IsItalic) fontStyle |= FontStyle.Italic; + if (model.IsStrickOut) fontStyle |= FontStyle.Strikeout; + if (model.IsUnderLine) fontStyle |= FontStyle.Underline; e.Appearance.Font = new Font(e.Appearance.Font, fontStyle); } } - catch (Exception) + catch (Exception ex) { - //LogHelper.Instance.WriteLog("GridView RowCellStyle Error. Condition:" + model.Condition + ";EvalCondtion:" + cond); + // 考虑增加错误日志记录 + // LogHelper.Instance.WriteLog("GridView RowCellStyle Error. Condition:" + item["condition"] + "; EvalCondtion:" + cond, ex); } } } } + /// + /// 解析列名是否为日期模式 + /// + /// + /// + /// + private bool IsDateColumn(string fieldName, out DateTime date) + { + // 通过正则表达式或其他方法来解析字段名是否包含日期,并尝试解析日期 + return DateTime.TryParse(fieldName, out date); // 示例,实际应根据实际字段名解析逻辑 + } + /// + /// 获取行中最大的有效日期 + /// + /// + /// + private DateTime? GetMaxDateFromColumnNames(DataRow row) + { + DateTime? maxDate = null; + foreach (DataColumn column in row.Table.Columns) + { + DateTime date; + if (IsDateColumn(column.ColumnName, out date)) + { + if (!maxDate.HasValue || date > maxDate.Value) + { + if (!string.IsNullOrEmpty(row[column.ColumnName]+"")) + { + maxDate = date; + } + } + } + } + return maxDate; + } + /// /// 说明:设置只读列 /// 创建人:龚宇超