Reputation: 26277
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?
Upvotes: 0
Views: 279
Reputation: 74530
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.
Upvotes: 1
Reputation: 317
That should work. Ideally, you should not need to call Invalidate when you change the BackColor property.
Are you sure normalColor != Color.Yellow ?
Upvotes: 0