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?

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,848 questions
C#
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.
10,355 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,196 Reputation points
    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;