From d30a3479352bea073a13f2d32f4dbea0207d2314 Mon Sep 17 00:00:00 2001 From: Abinesh p <120440951+abineshPalanisamy@users.noreply.github.com> Date: Tue, 26 May 2026 17:29:08 +0530 Subject: [PATCH 1/3] A fix has been added for the issue. A fix has been added for the issue. --- .../System/Windows/Forms/Controls/DataGridView/DataGridView.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/DataGridView/DataGridView.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/DataGridView/DataGridView.cs index 5f1af6e83c4..54d09c53b15 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/DataGridView/DataGridView.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/DataGridView/DataGridView.cs @@ -2042,6 +2042,9 @@ public DataGridViewCellStyle DefaultCellStyle // Update ambient font flag depending on cell style font _dataGridViewState1[State1_AmbientFont] = value?.Font == base.Font; + // FIX: if ForeColor is explicitly set, stop treating it as ambient + _dataGridViewState1[State1_AmbientForeColor] = value is null || value.ForeColor.IsEmpty; + DataGridViewCellStyleDifferences dgvcsc = cs.GetDifferencesFrom(DefaultCellStyle); if (dgvcsc != DataGridViewCellStyleDifferences.None) { From b5bd31cbef4c54573766275791403538376e3924 Mon Sep 17 00:00:00 2001 From: Abinesh p <120440951+abineshPalanisamy@users.noreply.github.com> Date: Fri, 26 Jun 2026 13:04:31 +0530 Subject: [PATCH 2/3] Added unit test cases for Fix_Issue_11065 Added unit test cases for Fix_Issue_11065 --- .../System/Windows/Forms/DataGridViewTests.cs | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/test/unit/System.Windows.Forms/System/Windows/Forms/DataGridViewTests.cs b/src/test/unit/System.Windows.Forms/System/Windows/Forms/DataGridViewTests.cs index 51e5f9b4b98..0baf5b35c42 100644 --- a/src/test/unit/System.Windows.Forms/System/Windows/Forms/DataGridViewTests.cs +++ b/src/test/unit/System.Windows.Forms/System/Windows/Forms/DataGridViewTests.cs @@ -4084,4 +4084,32 @@ public void DataGridView_Dispose_KeyboardToolTip_Disposed() dataGridView.Dispose(); toolTipDisposeCount.Should().Be(1); } + + [WinFormsFact] + public void DataGridView_DefaultCellStyle_SetWithForeColor_DoesNotInheritParentForeColor() + { + using Form form = new(); + using DataGridView control = new(); + + control.DefaultCellStyle = new DataGridViewCellStyle + { + Alignment = DataGridViewContentAlignment.MiddleLeft, + BackColor = SystemColors.Window, + Font = Control.DefaultFont, + ForeColor = Color.Lime, + SelectionBackColor = SystemColors.Highlight, + SelectionForeColor = SystemColors.HighlightText, + WrapMode = DataGridViewTriState.False + }; + + control.Columns.Add(new DataGridViewTextBoxColumn()); + int rowIndex = control.Rows.Add(); + + form.Controls.Add(control); + + form.ForeColor = Color.Red; + + Assert.Equal(Color.Lime, control.DefaultCellStyle.ForeColor); + Assert.Equal(Color.Lime, control.DefaultCellStyle.ForeColor); + } } From 8ff1e3f4d5883ce95abd7418c7ff4bc37ab8af4f Mon Sep 17 00:00:00 2001 From: Abinesh p <120440951+abineshPalanisamy@users.noreply.github.com> Date: Mon, 6 Jul 2026 15:46:54 +0530 Subject: [PATCH 3/3] Addressed the review correction for the Fix_Issue_11065 Addressed the review correction for the Fix_Issue_11065 --- .../Controls/DataGridView/DataGridView.cs | 2 +- .../System/Windows/Forms/DataGridViewTests.cs | 55 ++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/DataGridView/DataGridView.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/DataGridView/DataGridView.cs index 54d09c53b15..6c4941d5f52 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/DataGridView/DataGridView.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/DataGridView/DataGridView.cs @@ -2042,7 +2042,7 @@ public DataGridViewCellStyle DefaultCellStyle // Update ambient font flag depending on cell style font _dataGridViewState1[State1_AmbientFont] = value?.Font == base.Font; - // FIX: if ForeColor is explicitly set, stop treating it as ambient + // If ForeColor is explicitly set, stop treating it as ambient so Form.ForeColor doesn't override it. _dataGridViewState1[State1_AmbientForeColor] = value is null || value.ForeColor.IsEmpty; DataGridViewCellStyleDifferences dgvcsc = cs.GetDifferencesFrom(DefaultCellStyle); diff --git a/src/test/unit/System.Windows.Forms/System/Windows/Forms/DataGridViewTests.cs b/src/test/unit/System.Windows.Forms/System/Windows/Forms/DataGridViewTests.cs index 0baf5b35c42..1440d776393 100644 --- a/src/test/unit/System.Windows.Forms/System/Windows/Forms/DataGridViewTests.cs +++ b/src/test/unit/System.Windows.Forms/System/Windows/Forms/DataGridViewTests.cs @@ -4110,6 +4110,59 @@ public void DataGridView_DefaultCellStyle_SetWithForeColor_DoesNotInheritParentF form.ForeColor = Color.Red; Assert.Equal(Color.Lime, control.DefaultCellStyle.ForeColor); - Assert.Equal(Color.Lime, control.DefaultCellStyle.ForeColor); + } + + public void DataGridView_DefaultCellStyle_WithoutForeColor_InheritsParentForeColor() + { + using Form form = new(); + using DataGridView control = new(); + + control.DefaultCellStyle = new DataGridViewCellStyle + { + Alignment = DataGridViewContentAlignment.MiddleLeft, + BackColor = SystemColors.Window, + Font = Control.DefaultFont, + SelectionBackColor = SystemColors.Highlight, + SelectionForeColor = SystemColors.HighlightText, + WrapMode = DataGridViewTriState.False + }; + + control.Columns.Add(new DataGridViewTextBoxColumn()); + int rowIndex = control.Rows.Add(); + + form.Controls.Add(control); + + form.ForeColor = Color.Red; + + Assert.Equal(Color.Red, control.DefaultCellStyle.ForeColor); + Assert.Equal(Color.Red, control.Rows[rowIndex].Cells[0].InheritedStyle.ForeColor); + } + + [WinFormsFact] + public void DataGridView_DefaultCellStyle_EmptyForeColor_InheritsParentForeColor() + { + using Form form = new(); + using DataGridView control = new(); + + control.DefaultCellStyle = new DataGridViewCellStyle + { + Alignment = DataGridViewContentAlignment.MiddleLeft, + BackColor = SystemColors.Window, + Font = Control.DefaultFont, + ForeColor = Color.Empty, + SelectionBackColor = SystemColors.Highlight, + SelectionForeColor = SystemColors.HighlightText, + WrapMode = DataGridViewTriState.False + }; + + control.Columns.Add(new DataGridViewTextBoxColumn()); + int rowIndex = control.Rows.Add(); + + form.Controls.Add(control); + + form.ForeColor = Color.Red; + + Assert.Equal(Color.Red, control.DefaultCellStyle.ForeColor); + Assert.Equal(Color.Red, control.Rows[rowIndex].Cells[0].InheritedStyle.ForeColor); } }