tags:

views:

86

answers:

2

I have a datagridview on a form with an associated SelectionChanged event.

When the selected row contains "attachments" then the backcolor of the btnComments control should turn yellow as follows:

   int noAttachments = 1;

   if (noAttachments > 0)
       btnAttachments.BackColor = Color.Yellow;
   else
       btnAttachments.BackColor = normalColour;

   btnAttachments.Invalidate();

The code works as expected except that the Invalidate() call does nothing! i.e. the colour of the button does not change!

Any ideas why?

+1  A: 

I don't believe it is the call to Invalidate. If anything, Invalidate is not needed here, assuming that this button is a control. When you change the state of any control, the control is responsible for invalidating itself. So when you changed the BackColor property of the control, it should have invalidated itself which would trigger the repaint.

Are you sure that the BackColor property is supported for the control that btnAttachments is (I'm assuming it is a regular Button, but it might not be)?

Request for more info:

It also occurs to me that because this is a DataGridView, that you don't actually have the reference to the proper control that is being used to render the button. The DataGridView has a concept of templates for controls for a row/column/cell, and then the actual instance which it uses.

You need to show how you are getting the button for that particular row/cell/column.

casperOne
btnAttachments is a regular button control. Even with or without the Invalidate call, the colour of the button is not changing!
Calanus
@Calanus: Which suggests it is not the call to Invalidate. Are you sure that noAttachments is greater than 0? Are you sure that normalColour is not Yellow?
casperOne
normalColor is the standard colour of the button control - i.e. gray. I've stepped through the code and the btnAttachments.Color = Color.Yellow is definately firing...
Calanus
@Calanus: See the last two paragraphs that I added to the post, as I believe that is the source of your problem.
casperOne
What is the value of btnAttachments.BackColor after assignment? Are you sure it has changed?Aside: In case you suspect an invalidate problem, try a "manual" invalidate by obscuring your control with another window (even Notepad will do) and then uncovering it.
Vulcan Eager
A: 

That should work. Ideally, you should not need to call Invalidate when you change the BackColor property.

Are you sure normalColor != Color.Yellow ?

daanish.rumani