I'm trying to develop a WinForms application using C#. I'm using the DataGridView component and I want to add a column when a button is pressed. Additionally, when the mouse focus leaves the current area, I want to check if there are any empty cells in the DataGridView. If there are, I want to automatically delete the column containing that cell and sort the DataGridView. Here is my Code:
private void dataGridView4_Leave(object sender, EventArgs e)
{
// Create a list to store columns to be removed
List<DataGridViewColumn> columnsToRemove = new List<DataGridViewColumn>();
// Iterate through each column in the DataGridView (starting from index 1, skipping the first column)
for (int i = dataGridView4.Columns.Count - 1; i >= 1; i--)
{
DataGridViewColumn column = dataGridView4.Columns[i];
bool isEmpty = true;
// Iterate through all cells in the current column
foreach (DataGridViewRow row in dataGridView4.Rows)
{
// Check if the cell is empty
if (row.Cells[column.Index].Value != null && !string.IsNullOrWhiteSpace(row.Cells[column.Index].Value.ToString()))
{
// If at least one cell is not empty, set isEmpty to false and break out of the inner loop
isEmpty = false;
break;
}
}
// If all cells in the current column are empty, add the column to the list of columns to be removed
if (isEmpty)
{
columnsToRemove.Add(column);
}
}
// Remove the columns
foreach (var columnToRemove in columnsToRemove)
{
dataGridView4.Columns.Remove(columnToRemove);
}
// Refresh the dataGridView and rename/reorder the columns
RefreshDataGridViewIndex();
}
When I leave the area, there is no more blank columns, but I can't Close this Form.
The compiler doesn't provide any relevant error messages, but I can't close the Form. I don't know why this is happening. Can someone help me?
What's more, the following situation can also prevent me from closing the current Form.
In fact, this is exactly the effect I want this piece of code to achieve, that is, to delete the column to which the empty cell belongs when it exists. I hope this situation can provide more clues to help you locate the problem.
I tried to debug by reverse outputting or adding MessageBox.Show to locate where is the bug, but since the compiler didn't give me any clear error messages, I find it difficult to proceed. I can use other way or just give up my idea, but I just want to know why this happened.
---------------------------new clue------------------------
I just discovered a new clue, but I don't understand the mechanism behind the issue. The layout of my controls is as shown in the figure below.
Previously, I bound private void dataGridView4_Leave(object sender, EventArgs e) to the GroupBox instead of the DataGridView because I didn't want the behavior of clearing empty columns to trigger when I clicked on the GroupBox. I bound the event to the GroupBox's Leave event.
However, now that I've unbound the Leave event from the GroupBox and instead bound private void dataGridView4_Leave(object sender, EventArgs e) to the Mouse_Leave event of my DataGridView, it miraculously works correctly.
I'm wondering why this is the case. Are there any differences in the processing logic between the Leave event and Mouse_Leave event that would prevent me from closing my Form? I hope someone can help clarify my confusion. Thank you!
This might do SOMETHING but it feel very dirty
protected override void OnFormClosing(FormClosingEventArgs e)
{
e.Cancel = false;
base.OnFormClosing(e);
}