Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Give Cell a color based on its value

Status
Not open for further replies.

patrickstrijdonck

Programmer
Jan 18, 2009
97
NL
Hi,

This is probarly something simple.....

Ive got a Datagridview which gives me all open calls in the table.

It got 7 Columns which the 7th is the State column containing the SLA Calculation (which is done in a query in Access)

It can have 3 values: "Overdue!", "Warning!", "OK!"

each row can have a different SLA State, What im trying to work is that when the form opens, C# checks all rows and change the backcolor of the specific cell with that value to a color,

Every cell with the value "Overdue!" must turn red etc etc

What ive got now, makes the whole Datagridview the color RED :S

Code:
            int index = dataGridView1.RowCount;
            for (int i = 1; i < index; i++)
            {
                DataGridViewCell cell = dataGridView1[7, i];
                if (cell.Value.ToString() == "Overdue!")
                {
                    e.CellStyle.BackColor = Color.Red;
                }
                else if (cell.Value.ToString() == "Warning!")
                {
                    e.CellStyle.BackColor = Color.Orange;
                }
            }

Anything im missing here???
 
Patrick,

I'm not sure what you have "e" set to, but shouldn't you be changing the background color of the Cell?

try:
Code:
int index = dataGridView1.RowCount;
for (int i = 1; i < index; i++)
{
  DataGridViewCell cell = dataGridView1[7, i];
  if (cell.Value.ToString() == "Overdue!")
  {
    cell.Style.BackColor = Color.Red;
  }
  else if (cell.Value.ToString() == "Warning!")
  {
    cell.Style.BackColor = Color.Orange;
  }
}

The reference should hold to dataGridView1[7, 1] which is the cell you want to change.

I hope that does what I expect it to, and what you want it to.

Travis Hawkins
jobs.bestcodingpractices.com
 
E is the datagridview the code of that is this:
Code:
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

And while typing i see the failure and you are right, it must be \
Code:
cell.Style.Backcolor
and not
Code:
e.CellStyle.Backcolor

with the last piece of code, i say that the whole datagridview must be that backcolor, stupid i didnt see that.

i knew it was simple :)
 
I could just picture the poor grid flashing all the different background colors as it goes through each cell... could make a good disco affect :) Out of Error comes Innovation
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top