Devexpress 如何实现不同单元格显示内容颜色不同
gridView1.AddNewRow();
gridView1.Columns.AppearanceCell.ForeColor = Color.Red;
gridView1.SetRowCellValue(1, gridView1.Columns, "red");
gridView1.AddNewRow();
gridView1.Columns.AppearanceCell.ForeColor = Color.Green;
gridView1.SetRowCellValue(2, gridView1.Columns, "green");
以上代码,我以为会在第1行第0列显示的是红色,第2行,第0列显示的是绿色
结果是:都是绿色,若想实行第1行的是红色,第二行是绿色,该如何写代码呢?
var gridView = (GridView)myGridControl.MainView;
gridView.CustomDrawCell += delegate(object sender, RowCellCustomDrawEventArgs e)
{
var needSumList = new List<string>
{
"System.Int16",
"System.Int32",
"System.Int64",
"System.UInt16",
"System.UInt32",
"System.UInt64",
"System.Decimal",
"System.Double",
"System.Single"
};
if (needSumList.Contains(e.Column.ColumnType.ToString()))
{
var gridCellInfo = (GridCellInfo)e.Cell;
if (double.Parse(gridCellInfo.CellValue.ToString()) < 2000)
{
e.Appearance.BackColor = Color.Red;
}
}
}; 简单写了一下, 我这个是实现 只要列是数值类型且小于2000,则设置背景色为红色
你这个需求重点就是:你得重写 GridView中的CustomDrawCell方法,方法内可以获取到指定列的内容,然后你就可以根据条件设置字体颜色,背景色等等东西了 cloudrosa 发表于 2020-7-16 15:03
简单写了一下, 我这个是实现 只要列是数值类型且小于2000,则设置背景色为红色
你这个需求重点就是:你得 ...
谢谢 cloudrosa 研究了一个下午,找到了Devexpress设置单元格的办法,先贴个图让大家看看效果
单元格字体有三个颜色,红、绿、黑
红色字体放大
底色两种类型,单一底色,渐变颜色底色
以下是代码
gridView1.AddNewRow();
gridView1.SetRowCellValue(1, gridView1.Columns["Width"], "117");
gridView1.AddNewRow();
gridView1.SetRowCellValue(2, gridView1.Columns["Width"], "111");
gridView1.AddNewRow();
gridView1.SetRowCellValue(3, gridView1.Columns["Width"], "118");
gridView1.AddNewRow();
gridView1.SetRowCellValue(4, gridView1.Columns["Width"], "131");
gridView1.AddNewRow();
gridView1.SetRowCellValue(5, gridView1.Columns["Width"], "145");
gridView1.AddNewRow();
gridView1.SetRowCellValue(6, gridView1.Columns["Width"], "99");
gridView1.AddNewRow();
gridView1.SetRowCellValue(7, gridView1.Columns["Width"], "119");
DevExpress.XtraGrid.StyleFormatCondition redCondition = new DevExpress.XtraGrid.StyleFormatCondition();
redCondition.Appearance.Options.UseForeColor = true;
redCondition.Appearance.ForeColor = Color.Red;
redCondition.Appearance.Options.UseBackColor = true;
redCondition.Appearance.BackColor = Color.Yellow;
redCondition.Appearance.Options.UseFont = true;
redCondition.Appearance.Font = new Font("黑体", 13);
redCondition.Column = gridView1.Columns["Width"];
redCondition.Condition = DevExpress.XtraGrid.FormatConditionEnum.Expression;
redCondition.Expression = " > 120";
redCondition.Value1 = true;
DevExpress.XtraGrid.StyleFormatCondition blackCondition = new DevExpress.XtraGrid.StyleFormatCondition();
blackCondition.Appearance.Options.UseForeColor = true;
blackCondition.Appearance.ForeColor = Color.Black;
blackCondition.Appearance.Options.UseBackColor = true;
blackCondition.Appearance.BackColor = Color.Bisque;
blackCondition.Appearance.BackColor2 = Color.Green;
blackCondition.Column = gridView1.Columns["Width"];
blackCondition.Condition = DevExpress.XtraGrid.FormatConditionEnum.Expression;
blackCondition.Expression = " <= 120 And >= 115";
blackCondition.Value1 = true;
DevExpress.XtraGrid.StyleFormatCondition greenCondition = new DevExpress.XtraGrid.StyleFormatCondition();
greenCondition.Appearance.Options.UseForeColor = true;
greenCondition.Appearance.ForeColor = Color.Green;
greenCondition.Column = gridView1.Columns["Width"];
greenCondition.Condition = DevExpress.XtraGrid.FormatConditionEnum.Expression;
greenCondition.Expression = " <= 114";
greenCondition.Value1 = true;
this.gridView1.FormatConditions.AddRange(new DevExpress.XtraGrid.StyleFormatCondition[] {
redCondition,
blackCondition,
greenCondition
});
页:
[1]