Skip to content

Commit 31d5a9b

Browse files
committed
Add IsHandleCreated before invoking EndEdit
1 parent f0546b8 commit 31d5a9b

File tree

2 files changed

+4
-24
lines changed

2 files changed

+4
-24
lines changed

src/System.Windows.Forms/System/Windows/Forms/Controls/DataGridView/DataGridView.Methods.cs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14910,22 +14910,7 @@ protected virtual void OnDataSourceChanged(EventArgs e)
1491014910
/// <summary>
1491114911
/// Refresh items when the DataSource is disposed.
1491214912
/// </summary>
14913-
private void OnDataSourceDisposed(object? sender, EventArgs e)
14914-
{
14915-
try
14916-
{
14917-
// 1. Modify the state of the current operation to avoid unnecessary operations
14918-
// when setting DataSource and CurrentCell.
14919-
// 2. Setting CurrentCell to null then release DataSource.
14920-
_dataGridViewOper[OperationInReleasingDataSource] = true;
14921-
CurrentCell = null;
14922-
DataSource = null;
14923-
}
14924-
finally
14925-
{
14926-
_dataGridViewOper[OperationInReleasingDataSource] = false;
14927-
}
14928-
}
14913+
private void OnDataSourceDisposed(object? sender, EventArgs e) => DataSource = null;
1492914914

1493014915
protected virtual void OnDefaultCellStyleChanged(EventArgs e)
1493114916
{
@@ -27032,11 +27017,10 @@ protected virtual bool SetCurrentCellAddressCore(int columnIndex,
2703227017
int oldCurrentCellY = _ptCurrentCell.Y;
2703327018
if (oldCurrentCellX >= 0
2703427019
&& !_dataGridViewState1[State1_TemporarilyResetCurrentCell]
27035-
&& !_dataGridViewOper[OperationInDispose]
27036-
&& !_dataGridViewOper[OperationInReleasingDataSource])
27020+
&& !_dataGridViewOper[OperationInDispose])
2703727021
{
2703827022
DataGridViewCell currentCell = CurrentCellInternal;
27039-
if (!EndEdit(
27023+
if (IsHandleCreated && !EndEdit(
2704027024
DataGridViewDataErrorContexts.Parsing | DataGridViewDataErrorContexts.Commit | DataGridViewDataErrorContexts.CurrentCellChange,
2704127025
validateCurrentCell ? DataGridViewValidateCellInternal.Always : DataGridViewValidateCellInternal.Never,
2704227026
fireCellLeave: validateCurrentCell,

src/System.Windows.Forms/System/Windows/Forms/Controls/DataGridView/DataGridView.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ public partial class DataGridView : Control, ISupportInitialize
222222
private const int OperationInEndEdit = 0x00400000;
223223
private const int OperationResizingOperationAboutToStart = 0x00800000;
224224
private const int OperationTrackKeyboardColResize = 0x01000000;
225-
private const int OperationInReleasingDataSource = 0x02000000;
226225
private const int OperationMouseOperationMask = OperationTrackColResize | OperationTrackRowResize |
227226
OperationTrackColRelocation | OperationTrackColHeadersResize | OperationTrackRowHeadersResize;
228227
private const int OperationKeyboardOperationMask = OperationTrackKeyboardColResize;
@@ -1922,10 +1921,7 @@ public object? DataSource
19221921
newDataSource.Disposed += OnDataSourceDisposed;
19231922
}
19241923

1925-
if (CurrentCell is not null)
1926-
{
1927-
CurrentCell = null;
1928-
}
1924+
CurrentCell = null;
19291925

19301926
if (DataConnection is null)
19311927
{

0 commit comments

Comments
 (0)