DataGridViewComboBoxCell返回颜色更改

时间:2012-11-26 00:14:43

标签: winforms datagridview

DataGridViewComboBoxCell在应用程序启动时显示正常

enter image description here

选择值后,当前行和后续行的背景变为黑色(见下文)。我使用了默认的DataGridView而没有使用字体操作

enter image description here

我尝试在CellFormatting和CellMouseClick事件中更改颜色。但仍然是相同的行为。有什么想法吗?

2 个答案:

答案 0 :(得分:4)

显然,这是DataGridViewComboBoxColumn的文档错误。 Link with solution and workaround

答案 1 :(得分:0)

https://nickstips.wordpress.com/2010/12/20/c-datagridviewcomboboxcolumn-drop-down-menu-appears-all-black/

C#:DataGridViewComboBoxColumn 下拉菜单显示为全黑 2010 年 12 月 20 日 — 尼克奥尔森

我今天在使用 DataGridView 时遇到了一个问题,其中定义为 DataGridViewComboBoxColumn 的列之一出现,下拉菜单全黑,如下所示。

经过一些研究,我发现 DataGridViewComboBoxColumn 中有一个记录的错误,如果您正在处理 DataGridView 的 EditingControlShowing 事件,有时会发生这种情况。我正在处理此事件,以便连接嵌入在 DataGridView 单元格中的 ComboBox 的 SelectedIndexChanged 事件。

在错误报告中,Microsoft 表示他们不会修复此错误,但幸运的是,Debanjan1 已针对此问题发布了解决方法。如果您只是在 EditingControlShowing 事件中将 CellStyle.BackColor 属性设置为 DataGridView.DefaultCellStyle.BackColor,问题就会消失。如下所示。

private void dataGridViewGLEntries_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { ComboBox cmbBx = e.Control as ComboBox;

if (cmbBx != null)
{
    cmbBx.SelectedIndexChanged -= ComboBoxCell_SelectedIndexChanged;
    cmbBx.SelectedIndexChanged += ComboBoxCell_SelectedIndexChanged;

    // Fix the black background on the drop down menu
    e.CellStyle.BackColor = this.dataGridViewGLEntries.DefaultCellStyle.BackColor;
}

}