bernie2436
bernie2436

Reputation: 23921

Datagridview control's forecolor only changes after the event handler runs AND another cell is clicked

I want to change the color of a cell in a datagridview when the cell is doubleclicked. I've added a CellDoubleClick handler that fires properly after a cell is double clicked:

Private Sub myDataGridView_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles myDataGridView.CellDoubleClick
    myDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.ForeColor = Color.Red
    myDataGridView.Refresh()  'I added this to try to fix the problem
    Application.DoEvents()    'I added this to try to fix the problem
end sub

But after the event 'fires' the UI does not change the color of the cell's text immediately. Instead, if I single click a different cell it then Visual Studio 2010 changes the original cell to red (like the handler says to do).

It seems like there is some sort of UI refreshing/UI repainting going each time I click a different cell. Is this correct? Is there a way to refresh the UI programatically. Many stackoverflow posts advise .refresh but this is not working.

Upvotes: 0

Views: 474

Answers (1)

J...
J...

Reputation: 31443

This is because when you double click a cell it remains selected and the properties .SelectionForeColor and .SelectionBackColor are used. Only when you click another cell does it become de-selected and begin using .ForeColor and .BackColor. You can get it to update immediately by either also changing the .SelectionForeColor property or by setting .Selected = False immediately after.

Upvotes: 4

Related Questions