views:

265

answers:

2

I have a DataGridView populated with a BindingList. This list gets saved into the Settings file. It saves and loads correctly.. but will only save the first time I call Properties.Settings.Save(). After that, calling Save() will do nothing.

I have verified that Save() is being called, and that at the time it's called the BindingList contains the correct data. Regardless of whether I add or remove from, or edit elements in, the list, it will only save to user.config on the first call. After that, user.config is not even opened for editing.

Does anyone know why this would happen?

A: 

The call to Save sets the IsDirty property of the class representing the user property to false. This property is tested before saving the user property to disk.

If you need to call Save several times, set the IsDirty property to true:

Properties.Settings.Default.PropertyValues["property name"].IsDirty = true;
Timores
Nope, that still didn't do it. Any other ideas?
BlueRaja - Danny Pflughoeft
It definitely writes to the file on every call now, but it still only seems to write the information that was valid on the first call to `Save()`. Do I need to somehow mark the individual items in the `BindingList` as dirty? (The `BindingList` itself is the property)
BlueRaja - Danny Pflughoeft
What do you mean but "valid" information ?
Timores
I have it call `Save()` on the `CellEndEdit` event of the `DataGridView`, so that when I finish editing a cell, it saves. If I were to add three items to the DataGridView (which is bound to `Settings.Default.MyBindingList`), debugging confirms that the BindingList contains all three new values. `Settings.Default.Save()` is called all three times, and all three times, it opens user.config and writes to it. However, all three times it will only write the list with only the first item added.
BlueRaja - Danny Pflughoeft
A: 

I've accidentally come upon a solution, though I don't know why it works.

It seems calling Settings.Default.MyBindingList.ResetBindings() or Settings.Default.MyBindingList.ResetItem(1) (it does not matter what number is passed to ResetItem()) before calling Save() fixes the problem.

The BindingList must not have been marking its values as changed, even if the items in the list correctly implement INotifyPropertyChanged.

Remarkably, calling Settings.Default.MachineList.Machines.EndNew(1) also solves the problem (!?!). From the documentation:

The EndNew method commits a pending new item that was added through the AddNew method. If there is no new item waiting to be committed, then this method does nothing.

Can anyone shed some light on this? Am I doing something wrong, or is this a bug in either DataGridView or BindingList (or perhaps the Settings provider)?

BlueRaja - Danny Pflughoeft