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#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,591 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' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.