Devexpress GridView 数据格式化显示
Devexpress GridView 数据格式化显示
gridView1.CustomColumnDisplayText += gridView1_CustomColumnDisplayText; void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e) { if (e.Column.FieldName == "State") { switch (e.DisplayText) { case "0": e.DisplayText = "有效"; break; case "1": e.DisplayText = "无效"; break; } } }
void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e) { var currentView = sender as GridView; if (currentView != null && e.RowHandle == currentView.FocusedRowHandle) return; Rectangle r = e.Bounds; if (e.Column.FieldName == "F_State") { if (e.CellValue.ToString().Equals("False")) { e.Appearance.ForeColor=Color.Red; e.Appearance.DrawString(e.Cache,e.DisplayText,r); e.Handled = true; } } } 或者以下面的这种形式也可以的、 还有一个就是改变行的颜色 在对GridControl绑定好数据后: No.1:右键GridControl选择Run Designer; No.2:Appearance下Style Conditions点击Add,需要注意的是每一个变色条件都得Add一个变色方案; No.3:在Properties中需要用到的属性依次往下详解为: a)Appearance下BackColor=255.255.128,BackColor2=255.255.128,此项指定符合特定条件时单元格/行背景颜色,如果两项设置颜色不同时则为渐变效果; 要是每次都这样设置也太不方便了。。所以我又封装了一个方法
public void SetColumnFormatCell(object value1, object value2,Color backColor1,Color backColor2,GridColumn gridColumn,FormatConditionEnum formatType,GridView gridView) { var styleFormatCondition1 = new StyleFormatCondition(); styleFormatCondition1.Appearance.BackColor = backColor1; styleFormatCondition1.Appearance.BackColor2 =backColor2; styleFormatCondition1.Appearance.Options.UseBackColor = true; styleFormatCondition1.Column = gridColumn; styleFormatCondition1.Condition = formatType; styleFormatCondition1.Expression = "true"; styleFormatCondition1.Value1 = value1; styleFormatCondition1.Value2 = value2; gridView.FormatConditions.Add(styleFormatCondition1); }
调用:
var dev=new DataGridControlHelper(); dev.SetColumnFormatCell("无效","无效",Color.Red,Color.Red,gridColumn03,FormatConditionEnum.Equal,gridView1);
虽然效果是出来了。但是我觉得效率很差。 要是有那位网友有更好的方案。请分享一下。谢谢
作者:在水一方
出处:http://www.cnblogs.com/w2011
欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,谢谢