基于单元格值的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....