Click here to Skip to main content
14,826,929 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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.
Posted
Comments
lukeer 5-Jul-12 5:02am
   
Post the static value cell color change solution that you tried. Maybe we can combine it with some ValueChanged event.

Use the "Improve question" link for that.
Ahn_7 5-Jul-12 5:10am
   
For static value cell color change..Refer this Link http://atikpassion.blogspot.in/2011/10/wpf-datagrid-row-cell-color-change.html

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

C#
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex == 0 && e.RowIndex == 0)
        e.CellStyle.ForeColor = Color.Red;
}
   
Comments
Ahn_7 5-Jul-12 6:41am
   
I'm using WPF..So please suggest me a solution regarding WPF..
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.
   
v2
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....
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900