I recently migrated a Project from .NET 4
to .NET 4.7.2
which introduced a change in the WinForms DataGridView Headers.
Pre Migration looks like this:
As you can see, the Header of the cell i currently clicked is not selected. After the Migration the same DataGridView looks like this: I could not find any Information mentioning changes in the WinForms DataGridView at the Release Notes
I tried to set the Color using the following code from here how to change the color of winform DataGridview header?
this.dgvUserFields.ColumnHeadersDefaultCellStyle.BackColor = System.Drawing.SystemColors.ControlDark;
this.dgvUserFields.EnableHeadersVisualStyles = false;
But the code does not seem to change anything.
Are the some resources confirming that breaking change, and how to fix it ?
The behavior is documented in What's new in accessibility in the .NET Framework 4.7.2 in DataGridView improvements section:
When the
System.Windows.Forms.DataGridView.SelectionMode
is set toSystem.Windows.Forms.DataGridViewSelectionMode.FullRowSelect
, the column header will change color to indicate the current column as the user tabs through the cells in the current row.
In .NET Framework 4.7.2 when rendering DataGridViewColumnHeaderCell
, it checks if the column IsHighlighted
, then it renders the column in pushed state and here is the logic to detect IsHighlighted
:
private bool IsHighlighted()
{
return this.DataGridView.SelectionMode == DataGridViewSelectionMode.FullRowSelect &&
this.DataGridView.CurrentCell != null && this.DataGridView.CurrentCell.Selected &&
this.DataGridView.CurrentCell.OwningColumn == this.OwningColumn &&
AccessibilityImprovements.Level2;
}
As you can see in above code, there is && AccessibilityImprovements.Level2
. It means if you turn the feature off, the behavior will be reset.
As also mentioned in the comments by Taw, you can turn the feature off. To do so, you can add this block of setting to app.config
file:
<runtime>
<AppContextSwitchOverrides value="Switch.UseLegacyAccessibilityFeatures=false;Switch.UseLegacyAccessibilityFeatures.2=true" />
</runtime>
This work for me:
this.dgvUserFields.ColumnHeadersDefaultCellStyle.SelectionBackColor = this.dgvUserFields.ColumnHeadersDefaultCellStyle.BackColor;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With