6 Replies - 454 Views - Last Post: 17 May 2011 - 08:54 AM Rate Topic: -----

#1 DanDeCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-October 10

Formatting a row in datagridview

Posted 17 May 2011 - 04:25 AM

Guys, any reason why when the cell in the column 'sold' is set to 'No', the colour of the row still isn't changing to red?

Here's my code..

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            // Column is 'sold'
            // Looking to change all unsold vehicles to red

            if (this.dataGridView1.Columns[e.ColumnIndex].Name == "sold")
            {
                if (e.Value != null)
                {
                    string stringValue = (string)e.Value;
                    stringValue = stringValue.ToLower();

                    if (stringValue == "No")
                    {
                        e.CellStyle.BackColor = Color.Red;
                    }
                }
            }
        }


For info, I've tried changing the if statement to all variations of sold.. for example, _sold/Sold/sold. Still not working =(

Is This A Good Question/Topic? 0
  • +

Replies To: Formatting a row in datagridview

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1251
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Formatting a row in datagridview

Posted 17 May 2011 - 04:37 AM

Try clicking on another cell and clicking back in. I just had this same problem with another event last night and I solved it by using the CellEndEdit event.

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex > 1)
    {
        int tmpValue = Convert.ToInt32(dataGridView1[e.ColumnIndex, e.RowIndex].Value);

        if (tmpValue == 35)
        {
            dataGridView1[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Red;

        }
    }
}


Was This Post Helpful? 1
  • +
  • -

#3 DanDeCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-October 10

Re: Formatting a row in datagridview

Posted 17 May 2011 - 04:46 AM

Still not working :( have tried clicking out of the cell but still not changing to red. Could it be a property that needs changing perhaps?
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,356
  • Joined: 02-June 10

Re: Formatting a row in datagridview

Posted 17 May 2011 - 06:08 AM

stringValue = stringValue.ToLower();

                    if (stringValue == "No")



Because you are setting stringValue to lower case then comparing it against a mixed case condition.

"no" will never equal "No"
Was This Post Helpful? 3
  • +
  • -

#5 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1251
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Formatting a row in datagridview

Posted 17 May 2011 - 06:09 AM

Hahaha didn't even check that! :lol: Good eye! That's probably what's causing your issue.
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Formatting a row in datagridview

Posted 17 May 2011 - 08:27 AM

Change the code inside the if statement to this...

string value = Convert.ToString(e.Value);

if (value.Equals("No", StringComparison.InvariantCultureIgnoreCase))
{
    e.CellStyle.BackColor = Color.Red;
}



Was This Post Helpful? 0
  • +
  • -

#7 DanDeCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-October 10

Re: Formatting a row in datagridview

Posted 17 May 2011 - 08:54 AM

Trained eye :rockon: I knew it was something easy, but didn't think it would be that simple!.. Anyhow, nice one guys!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1