Skip to content
45 changes: 45 additions & 0 deletions src/test/integration/UIIntegrationTests/DataGridViewTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms.IntegrationTests.Common;
using Xunit.Abstractions;

namespace System.Windows.Forms.UITests;
Expand Down Expand Up @@ -40,6 +42,49 @@ await InputSimulator.SendAsync(
});
}

[WinFormsFact]
public void DataGridView_ClosesFormWhileDataGridViewInEditMode_WithBindingSource()
{
using Form form1 = new();
using Form form2 = new();
using DataGridView dataGridView = new();
using IContainer components = new Container();
using BindingSource bindingSource = new(components)
{
DataSource = TestDataSources.GetPersons()
};

dataGridView.DataSource = bindingSource;
dataGridView.Dock = DockStyle.Fill;
form2.Controls.Add(dataGridView);

form1.Shown += (_, _) =>
{
form2.Shown += (_, _) =>
{
// Ensure that DataGridView editing operations are performed after the form is fully displayed.
form2.BeginInvoke(() =>
{
// Close the form when the DataGridView enters edit mode.
void handler(object? s, DataGridViewEditingControlShowingEventArgs e)
{
dataGridView.EditingControlShowing -= handler;
form2.Close();
}

dataGridView.EditingControlShowing += handler;
dataGridView.CurrentCell = dataGridView.Rows[0].Cells[1];
dataGridView.BeginEdit(true);
});
};

form2.ShowDialog(form1);
form1.Close();
};

form1.ShowDialog();
}

[WinFormsTheory]
[InlineData("short value", false)]
[InlineData("very long value that will be truncated by the DataGridViewCell", true)]
Expand Down