Skip to content

Commit

Permalink
Merge pull request #1823 from giduac/1820-V85-kryptonDataGridView-doe…
Browse files Browse the repository at this point in the history
…s-not-add-krypton-columns-on-auto-generate

1820-V85-kryptonDataGridView-does-not-add-krypton-columns-on-auto-gen…
  • Loading branch information
Smurf-IV authored Oct 22, 2024
2 parents 3acdf42 + c78acfd commit 6356616
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Documents/Help/Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# <img src="https://github.com/Krypton-Suite/Standard-Toolkit/blob/master/Krypton.png?raw=true"> 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
* Implemented [#1813](https://github.com/Krypton-Suite/Standard-Toolkit/issues/1813), LTS Configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>Gets or sets the <see cref="T:System.Windows.Forms.ContextMenuStrip" /> associated with this control.</summary>
/// <returns>The <see cref="T:System.Windows.Forms.ContextMenuStrip" /> for this control, or <see langword="null" /> if there is no <see cref="T:System.Windows.Forms.ContextMenuStrip" />. The default is <see langword="null" />.</returns>
[Category(@"Behavior")]
Expand Down Expand Up @@ -1002,6 +1010,60 @@ protected virtual void OnButtonSpecChanged(object sender, [DisallowNull] EventAr
#endregion

#region Protected Override
/// <inheritdoc/>
protected override void OnDataMemberChanged(EventArgs e)
{
base.OnDataMemberChanged(e);

if (AutoGenerateColumns
&& AutoGenerateKryptonColumns
&& DataSource is not null)
{
ReplaceDefaultColumsWithKryptonColumns();
}
}

/// <inheritdoc/>
protected override void OnDataSourceChanged(EventArgs e)
{
base.OnDataSourceChanged(e);

if (AutoGenerateColumns
&& AutoGenerateKryptonColumns
&& DataSource is not null)
{
ReplaceDefaultColumsWithKryptonColumns();
}
}

/// <inheritdoc/>
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();
}
}

/// <inheritdoc/>
protected override void OnDataBindingComplete(DataGridViewBindingCompleteEventArgs e)
{
base.OnDataBindingComplete(e);

if (AutoGenerateColumns
&& AutoGenerateKryptonColumns
&& DataSource is not null)
{
ReplaceDefaultColumsWithKryptonColumns();
}
}

/// <summary>
/// Raises the PaintBackground event.
/// </summary>
Expand Down Expand Up @@ -1633,6 +1695,52 @@ internal bool RightToLeftInternal
#endregion

#region Implementation
/// <summary>
/// Handles the auto generation of Krypton columns<br/>
/// </summary>
private void ReplaceDefaultColumsWithKryptonColumns()
{
DataGridViewColumn currentColumn;
KryptonDataGridViewTextBoxColumn newColumn;
List<int> 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
Expand Down

0 comments on commit 6356616

Please sign in to comment.