.Net 4.0 和 .NET 4.7.2 标头选择之间 DataGridView 的重大变化

问题描述 投票:0回答:9

我最近将一个项目从

.NET 4
迁移到
.NET 4.7.2
,这在 WinForms DataGridView 标题中引入了更改。

预迁移如下所示:

如您所见,我当前单击的单元格的标题未被选中。 迁移后,相同的 DataGridView 如下所示: 我在 Release Notes

中找不到任何提及 WinForms DataGridView 中的更改的信息

我尝试使用此处的以下代码设置颜色 如何更改winform DataGridview标题的颜色?

this.dgvUserFields.ColumnHeadersDefaultCellStyle.BackColor = System.Drawing.SystemColors.ControlDark;
this.dgvUserFields.EnableHeadersVisualStyles = false;

但是代码似乎没有改变任何东西。

一些资源是否确认了重大更改,以及如何修复它?

c# winforms datagridview
9个回答
10
投票

该行为记录在 .NET Framework 4.7.2 中的辅助功能新增功能中 DataGridView 改进 部分:

System.Windows.Forms.DataGridView.SelectionMode
设置为
System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect
, 列标题将改变颜色以指示当前列为 用户通过 Tab 键浏览当前行中的单元格。

在 .NET Framework 4.7.2 中,渲染

DataGridViewColumnHeaderCell
时,它会检查列
IsHighlighted
是否存在,然后以推送状态渲染该列,以下是检测
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;
}

正如您在上面的代码中看到的,有

&& AccessibilityImprovements.Level2
。这意味着如果您关闭该功能,行为将被重置。

正如Taw的评论中也提到的,您可以关闭该功能。为此,您可以将此设置块添加到

app.config
文件:

<runtime>
    <AppContextSwitchOverrides value="Switch.UseLegacyAccessibilityFeatures=false;Switch.UseLegacyAccessibilityFeatures.2=true" />
</runtime>

3
投票

由于我不喜欢将 app.config 与独立可执行文件一起发布,因此我在 program.cs 中的 Main() 开头使用这些行来禁用“改进”:

AppContext.SetSwitch("Switch.UseLegacyAccessibilityFeatures.3", true);
AppContext.SetSwitch("Switch.UseLegacyAccessibilityFeatures.2", true);
AppContext.SetSwitch("Switch.UseLegacyAccessibilityFeatures", true);

1
投票

这对我有用:

this.dgvUserFields.ColumnHeadersDefaultCellStyle.SelectionBackColor = this.dgvUserFields.ColumnHeadersDefaultCellStyle.BackColor;

1
投票

这对我有用。

dataGridView1.ColumnHeadersDefaultCellStyle.SelectionBackColor = dataGridView1.ColumnHeadersDefaultCellStyle.BackColor;

1
投票

这对我有用。在网格的“属性”窗口中:

  1. 将所需的 ColumnHeaderDefaultCellStyle 设置为您希望标题的外观。
  2. 设置EnableHeaderVisualStyles = false

1
投票

dgvUserFields.EnableHeadersVisualStyles = false;

在表单加载事件中添加此


0
投票

补充Reza的答案

随着 .NET Framework 4.8 的出现,出现了新的可访问性改进级别:

AccessibilityImprovements.Level3

因此,将其添加到您的 app.config 时:

<runtime>
    <AppContextSwitchOverrides value="Switch.UseLegacyAccessibilityFeatures=false;Switch.UseLegacyAccessibilityFeatures.2=true" />
</runtime>

那么您可能会在应用程序启动期间遇到此异常:

System.NotSupportedException:“桌面应用程序需要选择所有早期的辅助功能改进才能获得以后的改进。为此,请确保如果 AppContext 开关“Switch.UseLegacyAccessibilityFeatures.N”设置为“false”,则“Switch.UseLegacyAccessibilityFeatures”和所有“Switch.UseLegacyAccessibilityFeatures.M”开关(当 M < N, evaluate to false as well. Note that, if a switch for a particular set of accessibility improvements is not present, its value is determined by the target framework version. You can remedy this by adding these switches and setting their value to false.'

时)

要解决此问题,您还必须选择退出第 3 级:

<runtime>
    <AppContextSwitchOverrides value="Switch.UseLegacyAccessibilityFeatures.2=true;Switch.UseLegacyAccessibilityFeatures.3=true" />
</runtime>

请注意:

如果您已应用 .NET Framework 4.8 的 KB4569746 累积更新,那么您可能也必须选择退出级别 4。


0
投票

禁用“改进”确实有效,但由于某种原因导致了其他问题。

我有一些存在问题的表格,随着应用程序的增长,我还会添加更多表格。因此,我创建了自己的自定义 DataGridView 来处理 DataGridView 的 ColumnStateChanged 事件,并将 HeaderCell 的样式重置为默认背景值,这样我就不必在每个新表单上添加此行/句柄事件。

  1. 创建一个供您的应用程序使用的自定义 DataGridView。
  2. 在构造函数中,您可以处理DataGridView的ColumnStateChanged事件并将HeaderCell的Style重置为默认背景值:

例如(实际上,由于搜索过滤等其他原因,我已经有了一个自定义网格):

public class DataGridViewCustom : DataGridView
  {
    public DataGridViewSearch() : base()
    {
      InitializeComponent();
  
      this.ColumnStateChanged += DataGridViewCustom_ColumnStateChanged;
    }
  
    private void DataGridViewCustom_ColumnStateChanged(object sender, DataGridViewColumnStateChangedEventArgs e)
    {
      DataGridView dataGridView = (DataGridView)sender;
      //Only update for full row selection mode.
      if (dataGridView.SelectionMode == DataGridViewSelectionMode.FullRowSelect)
      {
        e.Column.HeaderCell.Style.SelectionBackColor = dataGridView.ColumnHeadersDefaultCellStyle.BackColor;
      }
    }
  }

0
投票

只需添加一个 Cell_Painting 事件处理程序并设置回原始颜色:

在此示例中,我检查 e.FormattedValue 是否与我要删除突出显示的列标题的文本匹配。然后我简单地设置回原来的颜色。

    private void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        if (e.FormattedValue == "HeaderTextValue")
        {
            e.CellStyle.SelectionBackColor = Color.FromArgb(240, 240, 240, 240);
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.