基于单元格值的DataGrid单元格背景色或前景色 [英] DataGrid Cell Background Color or Foreground Color Based On Cell Value

查看:81
本文介绍了基于单元格值的DataGrid单元格背景色或前景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的主题本身就是我的问题..我需要更改Datagrid中的单元格背景色或前景色取决于单元格中的值.但是这里的值不是硬编码的,它会动态更改(15.56或25.30或某些double值).

注意:

我用Google搜索了一个解决方案,但只有静态值单元格颜色更改解决方案..请帮助我找到解决此问题的解决方案.

问候,
Ahilan.

Hi,

My subject itself tells my issue..What I need is that I want to change my Cell Background Color or Foreground Color in my Datagrid depends on my value in the cell..But here my value is not hard-coded, it will change dynamically ( 15.56 or 25.30 or some double values )..

Note :

I googled for a solution but i got only static value cell color change solution..Please help me to find solution for this issue..


Regards,
Ahilan.

推荐答案

这很简单.您可以遍历单元格,也可以像下面这样直接进行设置.

It''s very simple. You can either loop through cells or directly set it like below.

this.dataGridView1.Rows[0].Cells[1].Style.ForeColor = System.Drawing.Color.Red;



如果您希望它动态发生,请编写以下事件.例如



If you wan''t it to happen dynamically, code the below event. For example

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex == 0 && e.RowIndex == 0)
        e.CellStyle.ForeColor = Color.Red;
}


> http: //stackstackflow.com/questions/5549617/change-datagrid-cell-colour-based-on-values [
http://stackoverflow.com/questions/5549617/change-datagrid-cell-colour-based-on-values[^]


Take look of above example see marked answer. It will definately work for you.


这将解决您的问题

http://social.msdn.microsoft.com/Forums/zh_cn/wpf/thread/28b6750b-d715-474f-b5b8-a2c6653ea6ca


如果适合您,请标记为解决方案....
This will solve your issue

http://social.msdn.microsoft.com/Forums/en/wpf/thread/28b6750b-d715-474f-b5b8-a2c6653ea6ca


Mark as solution if it works for you....


这篇关于基于单元格值的DataGrid单元格背景色或前景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆