Click here to Skip to main content
16,016,580 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a winform app with a datagridview. I edit text a cell and save it. which works ok. But when I want to close the form, the app does not work.

What I have tried:

The form closing event is executed, but the form closed event is not
Posted
Comments
Asha Love 15-Jul-24 12:40pm    
When I click on a cell and it goes into edit mode, I can close the app
Dave Kreskowiak 15-Jul-24 13:13pm    
"It doesn't work" is the most useless problem description ever invented. Describe the problem in detail. What do you mean by "the app doesn't work"? What exactly are you expecting to happen and what actual does happen? Any error messages?
Asha Love 15-Jul-24 13:32pm    
No error occurs. Form does not close when the X button is pressed.
But the X button works when a cell is clicked to enter edit mode
Dave Kreskowiak 15-Jul-24 14:16pm    
So, what's in the Form_Closing and Form_Closed event handlers?
Asha Love 15-Jul-24 14:32pm    
private void Frm_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
{
if (System.Windows.Forms.MessageBox.Show("Are you sure?", "", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
{
e.Cancel = true;
return;
}
}

private void Frm_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
{
System.Windows.Forms.MessageBox.Show("Test");
}

It sounds to me like you have form validation present that is preventing the form from closing and editing in the grid fixes the invalid data, which allows you to continue and close the form. @Chill60 wrote an excellent tip that demonstrates working around this for a DataGridView. Allow Form to close when invalid data is present[^]
 
Share this answer
 
Comments
Asha Love 16-Jul-24 3:27am    
It really worked. Thank you. I have not encountered anything like this before. Although the true value was not given for e.Cancel, but by putting the code e.Cancel = false; At the end of the FormClosing form, the program was closed without any problems.
thanks guys
Pete O'Hanlon 16-Jul-24 3:37am    
I'm glad you got it solved. It's definitely an odd one.
Without better information it is difficult to be sure but I suspect that the FormClosing event handler is receiving an events args with Cancel already set to true.

This only time I have seen this happen is when a child control on the form has failed validation.
 
Share this answer
 
Start by using the debugger: put a breakpoint at the top of each handler method and when they hit look at the FormClosingEventArgs.Cancel property in the first.

If you look at the documentation: FormClosingEventArgs Class (System.Windows.Forms) | Microsoft Learn[^] it says:
Quote:
If a form has any child or owned forms, a FormClosing event is also raised for each one. If any one of the forms cancels the event, none of the forms are closed. Therefore the corresponding FormClosed events are not sent to any of the forms.
So check what other forms you have open and see if any of them are canceling the close.
 
Share this answer
 
Comments
Asha Love 16-Jul-24 2:42am    
The form that contains the gridview is the child form.
Exactly when this happens, the parent form is not closed.
The source of all these problems is Gridview. When a cell is clicked and that cell is edited, the problem is solved.
Sorry if I can't explain better, my English is poor.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900