How can I prevent the DataGridView header column from being highlighted when a row is selected

Pat Hanks 141 Reputation points
2023-04-24T13:29:00.0566667+00:00

I am using Visual Studio 17.5.4 and building a Windows Forms app with a DataGridView as a list of items to select then populate the form fields.  All this works but I have a nagging problem to overcome.  When you select a row from the grid, it will highlight the column header for whichever cell that was clicked, or if you click the row header panel of the row it will highlight the first column. User's image

I am currently using the SelectionChanged event to capture the selection but have tried CellClick & RowEnter with the same result.  Are there any suggestions on how to prevent the column header from changing color?

Developer technologies Windows Forms
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2023-04-24T15:31:50.2666667+00:00

    Here is a solution but will prevent sorting via the column header

    someDataGridView.EnableHeadersVisualStyles = false;
    someDataGridView.ColumnHeadersDefaultCellStyle.SelectionBackColor =
        someDataGridView.ColumnHeadersDefaultCellStyle.BackColor;
    
    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.