你的位置:首页 > ASP.net教程

[ASP.net教程]Devexpress GridView 数据根据不同条件格式化显示色块和文字颜色

1.色块和文字颜色方法

/// <summary>
/// 设置单元格样式(色块)
/// </summary>
/// <param name="gc">Devexpress GridView </param>
/// <param name="color">颜色</param>
/// <param name="expression">表达式(条件)</param>
/// <returns></returns>
protected virtual StyleFormatCondition SetStyleFormatCondition(GridColumn gc, Color color, string expression)
{
StyleFormatCondition condition1 = new DevExpress.XtraGrid.StyleFormatCondition();
condition1.Column = gc;
condition1.Appearance.BackColor = color;
condition1.Appearance.Options.UseBackColor = true;
condition1.Condition = FormatConditionEnum.Expression;
condition1.Expression = expression;
return condition1;
}

/// <summary>
/// 设置单元格(字体颜色)
/// </summary>
/// <param name="gc">Devexpress GridView</param>
/// <param name="color">颜色</param>
/// <param name="expression">表达式(条件)</param>
/// <returns></returns>
protected virtual StyleFormatCondition SetStyleFormatFontCondition(GridColumn gc, Color color, string expression)
{
StyleFormatCondition condition1 = new DevExpress.XtraGrid.StyleFormatCondition();
condition1.Column = gc;
condition1.Appearance.ForeColor = color;
condition1.Appearance.Options.UseForeColor = true;
condition1.Appearance.Options.UseTextOptions = true;
condition1.Condition = FormatConditionEnum.Expression;
condition1.Expression = expression;
return condition1;
}

2.开始调用

gvData.FormatConditions.Add(SetStyleFormatCondition(gvData.Columns["AuditStatus"], Color.FromArgb(56, 173, 40), "AuditStatus=='已审核'"));//审核状态(色块)

gvData.FormatConditions.Add(SetStyleFormatFontCondition(gvData.Columns["OrderStatus"], Color.FromArgb(30, 119, 194), "OrderStatus=='未完成'"));//执行状态(文字颜色)

执行结果: