Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > .EndEdit() within .FormClosing eventhandler not processed ?
 

.EndEdit() within .FormClosing eventhandler not processed ?

Hi, having a hard time with bindingsource.EndEdit() and _FormClosing event of my WinForm.

Scenario:

- dataset (ds) loaded via ds.ReadXml(<filename>)
- bindingsource (bs) bs.Datasource = ds
- datagridview (dgv) dgv.Datamember pointing to a table in dataset ds.

so far so good, I do see the data....

Upon closing the form (red X) I would like to see if there are pending changes.

Therefor in my _FormClosing event handler I
- Call first bs.EndEdit()
- Test dataset: ds.HasChanges()


Observation:

- When I change a cell in my datagridview and click "X" before leaving the datagridview: HasChanges() is false ---> Not OK !
- When I change a cell in my datagridview, shift focus to another control and then click "X": HasChanges() is true --> OK

Things I tried:
- When doing the same in a click event handler of another button: HasChanges() gives in both cases: TRUE (correct)
- When I put in my _FormClosing eventhandler a statement like: <another control>.focus = true, all is also fine.

Seems that .EndEdit() in _FormClosing event handlers are not processed correctly ? or did I do something stupid :-)

Thanks for your input...

Boris

p.s. Using Visual C# 2008 Express

  • Moved byVMazurMVPWednesday, September 30, 2009 4:40 AM (From:ADO.NET DataSet)
  •  
boris1001  Saturday, May 30, 2009 3:25 PM
I could not reproduce the problem. I am observing that the DataGridView row ends its editing when the close X is pressed. Not even bs.EndEditwas necessary to ensure that the grid's changes were committed in the DataSet.

Windows forms data binding is complex and imperfect. Don't feel too badly if you need to use some duct tape workarounds here and there. (If you still find that you need a workaround and are unhappy with the focus change, there is also the DataGridView.EndEdit method that you can look into.)

BinaryCoder  Saturday, May 30, 2009 5:18 PM
Thanks for the suggesting, however I did try that also...actually that was my first implementation (didn't used a bindingsource atall). Because DatagridView.EndEdit() didn't seemed to work I changed to bindingsource and bindingsource.EndEdit().

Did perform another test, upon closing the Form ("X") I always saved the bounded dataset to XML and checked if the "editted" cell shows up correcdt in XML: it didn't..so it is not the case that .HasChanges() is wrong. There seems to be no communication back from the datagridview to the dataset in this particular case.

Would there be any event I could subscribe to and inspect some interim datato get to the root cause ?

Bandage solution by setting focus to another control when closing the form is a bit too awkward .....

Thanks,

Boris
boris1001  Saturday, May 30, 2009 5:44 PM

Hi, having a hard time with bindingsource.EndEdit() and _FormClosing event of my WinForm.

Scenario:

- dataset (ds) loaded via ds.ReadXml(<filename>)
- bindingsource (bs) bs.Datasource = ds
- datagridview (dgv) dgv.Datamember pointing to a table in dataset ds.

so far so good, I do see the data....

Upon closing the form (red X) I would like to see if there are pending changes.

Therefor in my _FormClosing event handler I
- Call first bs.EndEdit()
- Test dataset: ds.HasChanges()


Observation:

- When I change a cell in my datagridview and click "X" before leaving the datagridview: HasChanges() is false ---> Not OK !
- When I change a cell in my datagridview, shift focus to another control and then click "X": HasChanges() is true --> OK

Things I tried:
- When doing the same in a click event handler of another button: HasChanges() gives in both cases: TRUE (correct)
- When I put in my _FormClosing eventhandler a statement like: <another control>.focus = true, all is also fine.

Seems that .EndEdit() in _FormClosing event handlers are not processed correctly ? or did I do something stupid :-)

Thanks for your input...

Boris

p.s. Using Visual C# 2008 Express

I used the following in C# 2005 FormClosing Event

if (dgv1.IsCurrentCellInEditMode)
{
dgv1.CurrentCell = null;
}
DataTable dtChanges = dtFormData.GetChanges();
if (dtChanges != null && dtChanges.Rows.Count > 0)
{
// found changes
MessageBox.Show("have changes");
}
This works for me.
JW429  Thursday, September 24, 2009 3:38 AM

You can use google to search for other answers

Custom Search

More Threads

• DataGrid Column Width Problem
• How to add new items in combobox in DataGridView
• DataGridView - Moving around with Enter/Tab/Arrows
• Keystroke to sort datagrid
• How to prevent DataGridView from selecting row on sort?
• Combobox bound to an ArrayList in databound grid
• How to limit the binding navigator addnew to 1 item
• VB2005 filter using datetimepicker
• Treestyleview in a datagridview?
• accessing a parent record using windows forms binding.