'Hovering over a DataGridViewComboBoxColumn in a WinForms app in Windows 11 turns cell black (OK in Windows 10)
When we run our application on Windows 11 the DataGridView control seems to act differently. Hovering over a cell in a DataGridViewComboBoxColumn turns the cell black making any text in that cell unreadable (see image). Windows 10 is OK. Screenshot of datagridview
Any ideas why this control acts differently in Windows 11. And if so, is it permanent? I could add some code in to try and fix this, but so far my efforts (below) have not worked.
private void DataGridView_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex >= 0 && e.RowIndex >= 0 && dataGridView.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn)
{
dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.FromArgb(220, 220, 255);
}
}
private void DataGridView_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex >= 0 && e.RowIndex >= 0 && dataGridView.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn)
{
dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = SystemColors.ControlLight;
}
}
Solution 1:[1]
Extending the answer already given by @TnTinMn in the question's comments above, you could fix this for all DataGridViewComboBoxColumn columns in your application by implementing a custom DataGridView control and overriding the OnColumnAdded method:
public class CustomDataGridView : DataGridView
{
protected override void OnColumnAdded(DataGridViewColumnEventArgs e)
{
if (e.Column is DataGridViewComboBoxColumn comboColumn && comboColumn.FlatStyle == FlatStyle.Standard)
{
comboColumn.FlatStyle = FlatStyle.Flat;
}
base.OnColumnAdded(e);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Kev |