Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breaking Change in DataGridView between .Net 4.0 and .NET 4.7.2 Header Selection

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: enter image description here

As you can see, the Header of the cell i currently clicked is not selected. After the Migration the same DataGridView looks like this: enter image description here 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 ?

like image 861
quadroid Avatar asked Jul 25 '18 07:07

quadroid


2 Answers

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 to System.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>
like image 164
Reza Aghaei Avatar answered Sep 16 '22 21:09

Reza Aghaei


This work for me:

this.dgvUserFields.ColumnHeadersDefaultCellStyle.SelectionBackColor = this.dgvUserFields.ColumnHeadersDefaultCellStyle.BackColor;
like image 20
Lubko Avatar answered Sep 20 '22 21:09

Lubko