From 117cbfece85877852f8c90d2661c659d4977e68f Mon Sep 17 00:00:00 2001 From: Giduac Date: Sun, 20 Oct 2024 16:18:09 +0200 Subject: [PATCH 1/2] 1820-V85-kryptonDataGridView-does-not-add-krypton-columns-on-auto-generate Resolves the auto generate issue with Krypton columns And the change log --- Documents/Help/Changelog.md | 1 + .../Controls Toolkit/KryptonDataGridView.cs | 108 ++++++++++++++++++ 2 files changed, 109 insertions(+) diff --git a/Documents/Help/Changelog.md b/Documents/Help/Changelog.md index 120bf6efc..5cb9168fe 100644 --- a/Documents/Help/Changelog.md +++ b/Documents/Help/Changelog.md @@ -3,6 +3,7 @@ ======= # 2024-10-14 - Build 2410 (Patch 3) - October 2024 +* Resolved [#1820](https://github.com/Krypton-Suite/Standard-Toolkit/issues/1820), When KryptonDataGridView.AutoGenerate is set Winforms columns are used. See the issue for full text. * Implemented [#1813](https://github.com/Krypton-Suite/Standard-Toolkit/issues/1813), LTS Configuration * Resolved [#1800](https://github.com/Krypton-Suite/Standard-Toolkit/issues/1800) `KryptonDataGridViewComboBoxEditingControl.EditingControlFormattedValue` property is differently implemented. * Implemented [#1792](https://github.com/Krypton-Suite/Standard-Toolkit/issues/1792), Enable 'SourceLink' for NuGet packages diff --git a/Source/Krypton Components/Krypton.Toolkit/Controls Toolkit/KryptonDataGridView.cs b/Source/Krypton Components/Krypton.Toolkit/Controls Toolkit/KryptonDataGridView.cs index bd654248a..6c22c0608 100644 --- a/Source/Krypton Components/Krypton.Toolkit/Controls Toolkit/KryptonDataGridView.cs +++ b/Source/Krypton Components/Krypton.Toolkit/Controls Toolkit/KryptonDataGridView.cs @@ -350,6 +350,14 @@ protected override void Dispose(bool disposing) #endregion #region Public + [Browsable(false)] + [Description(@"When true and AutoGenerateColumns is true the KryptonDataGridView will use Krypton column types, when false the standard WinForms column types.")] + [DefaultValue(true)] + public bool AutoGenerateKryptonColumns { + get; + set; + } = true; + /// Gets or sets the associated with this control. /// The for this control, or if there is no . The default is . [Category(@"Behavior")] @@ -1002,6 +1010,60 @@ protected virtual void OnButtonSpecChanged(object sender, [DisallowNull] EventAr #endregion #region Protected Override + /// + protected override void OnDataMemberChanged(EventArgs e) + { + base.OnDataMemberChanged(e); + + if (AutoGenerateColumns + && AutoGenerateKryptonColumns + && DataSource is not null) + { + ReplaceDefaultColumsWithKryptonColumns(); + } + } + + /// + protected override void OnDataSourceChanged(EventArgs e) + { + base.OnDataSourceChanged(e); + + if (AutoGenerateColumns + && AutoGenerateKryptonColumns + && DataSource is not null) + { + ReplaceDefaultColumsWithKryptonColumns(); + } + } + + /// + protected override void OnAutoGenerateColumnsChanged(EventArgs e) + { + // First handle the base the event + base.OnAutoGenerateColumnsChanged(e); + + // If needed convert the winforms columns to Krypton columns + if (AutoGenerateColumns + && AutoGenerateKryptonColumns + && DataSource is not null) + { + ReplaceDefaultColumsWithKryptonColumns(); + } + } + + /// + protected override void OnDataBindingComplete(DataGridViewBindingCompleteEventArgs e) + { + base.OnDataBindingComplete(e); + + if (AutoGenerateColumns + && AutoGenerateKryptonColumns + && DataSource is not null) + { + ReplaceDefaultColumsWithKryptonColumns(); + } + } + /// /// Raises the PaintBackground event. /// @@ -1633,6 +1695,52 @@ internal bool RightToLeftInternal #endregion #region Implementation + /// + /// Handles the auto generation of Krypton columns
+ ///
+ private void ReplaceDefaultColumsWithKryptonColumns() + { + DataGridViewColumn currentColumn; + KryptonDataGridViewTextBoxColumn newColumn; + List columnsProcessed = []; + int index; + + for (int i = 0 ; i < ColumnCount ; i++) + { + currentColumn = Columns[i]; + + /* + * Auto generated columns are always of type System.Windows.Forms.DataGridViewTextBoxColumn. + * Only columns that are of type DataGridViewTextBoxColumn and have the DataPropertyName set will be converted to krypton Columns. + */ + if (currentColumn is DataGridViewTextBoxColumn && currentColumn.DataPropertyName.Length > 0) + { + index = currentColumn.Index; + columnsProcessed.Add(index); + + newColumn = new KryptonDataGridViewTextBoxColumn + { + Name = currentColumn.Name, + DataPropertyName = currentColumn.DataPropertyName, + HeaderText = currentColumn.HeaderText, + Width = currentColumn.Width + }; + + Columns.RemoveAt(index); + Columns.Insert(index, newColumn); + } + } + + /* + * After the columns have been replaced they need a little help so they have the same width as when only Winforms columns would've been auto added. + * Setting this value in the above for loop does not work. + */ + for (int i = 0 ; i < columnsProcessed.Count ; i++) + { + Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells; + } + } + private void SetupVisuals() { // Setup the invoke used to refresh display From c78acfdd02c7261eb82ccb2ba6dc0e72f1759760 Mon Sep 17 00:00:00 2001 From: Giduac Date: Mon, 21 Oct 2024 08:59:56 +0200 Subject: [PATCH 2/2] 1820-V85-kryptonDataGridView-does-not-add-krypton-columns-on-auto-generate Adds new section to the change log --- Documents/Help/Changelog.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Documents/Help/Changelog.md b/Documents/Help/Changelog.md index 5cb9168fe..961c0eff0 100644 --- a/Documents/Help/Changelog.md +++ b/Documents/Help/Changelog.md @@ -1,9 +1,10 @@ # Standard Toolkit - ChangeLog ======= +# 2024-11-14 - Build 2411 (Patch 4) - November 2024 +* Resolved [#1820](https://github.com/Krypton-Suite/Standard-Toolkit/issues/1820), When KryptonDataGridView.AutoGenerate is set Winforms columns are used. See the issue for full text. # 2024-10-14 - Build 2410 (Patch 3) - October 2024 -* Resolved [#1820](https://github.com/Krypton-Suite/Standard-Toolkit/issues/1820), When KryptonDataGridView.AutoGenerate is set Winforms columns are used. See the issue for full text. * Implemented [#1813](https://github.com/Krypton-Suite/Standard-Toolkit/issues/1813), LTS Configuration * Resolved [#1800](https://github.com/Krypton-Suite/Standard-Toolkit/issues/1800) `KryptonDataGridViewComboBoxEditingControl.EditingControlFormattedValue` property is differently implemented. * Implemented [#1792](https://github.com/Krypton-Suite/Standard-Toolkit/issues/1792), Enable 'SourceLink' for NuGet packages