BindingSource.EndEdit() doesn't write values to datasource - c#

I had hoped that the EndEdit() method would take all bindings and write the control values values back to the datasource. Suprisingly, it doesn't always work. Sometimes the values are not written back to the datasource.
When I want to ensure that the controls write their data back to the datasource, is my only option to get each control's databinding and call WriteValue()?
Any other thoughts or ideas?
UPDATE
I bind to an object.
I have a textbox and I handle the TextChanged event. Within this handler I call EndEdit().
When I bind to the datasource, the TextChanged event is called which triggers the EndEdit(). If I then type into the textbox, the FIRST call to EndEdit() has no effect. After that it seems to work.
Note: I can get this to work if I do not have a TextChanged handler until AFTER the data is bound to the TextBox. It seems that the first EndEdit call (during the binding) seems to have a bad effect.

EndEdit() only works if binding source is in editing state, which is usually triggered by entering into control or starting actual editing.

Related

Differenciating selection change depending on call from code or changed by hand

I would like to know if it is possible to make a difference in the event selection change of a combobox.
I want to make a difference between a user that manually click the combobox and changes its value and a change in the selection that i do from code.
ie :
if i click my combobox and change its value by hand, the event is fired
but if i do myCombobox.selectedItem=1 [edit] the events is not fired
Is there an event that has this behaviour in a wpfcombobox ?
If not, do you have any idea on how to do that ?
Thanks
[edit] or if it is the binding of the combobox that changes its value
You are dealing with a couple of different scenarios, both of which are solvable.
1) Don't process SelectedItem requests during databinding. You have at least two options here:
a) Don't add the event handlers to the control until after databinding is complete or the form is loaded (depending on whether or not databinding is automatic or manual).
b) Set a form level property indicating when it is ok to process the SelectedItem event. You will probably want to set this to true after the form is loaded or after the databinding is complete. In your SelectedItem code, don't perform any actions unless this property is true.
2) Process the SelectedItem logic if the SelectedItem is changed programatically. Again, two options:
a) Extract your logic from the SelectedItem event into a method and then call this method when you perform the logic to set the selected item.
b) Create a custom combobox that inherits from the base and add a SetSelectedItem method (for example) to this inherited combo. This method would then raise the SelectedItem method. This would be reusable and you wouldn't have to remember to do 2 pieces of work whenever you set the SelectedItem manually.
Before you change the selecteditem in code, remove the event handler with -=, then add it back afterwards.

What Textbox event is raised right after bound data source is updated?

In windows forms, when I tab out of a text box, the bound data source value is updated. I'd like to capture the events right before and right after the data source changed. I think the OnLeave event is what I want for the before event. In the debugger, I'm not seeing the data source value changed. But, what event can I key off of for the after event?
I don't think there is an event that does exactly what you're asking, the closest I think you're going to get is to use the DataBindings and find your specific Binding and the you can catch the Parse event. But I believe this event fires before the data is pushed back to the source, so it's not much better than the LostFocus event.
The default event for TextBox DataBindings is DataSourceUpdateMode.OnValidation. When you tab out of the TextBox, the following events will fire:
Leave
Validating
(data source gets updated)
Validated
The Validating event has a CancelEventArgs parameter that allows you to cancel the leave attempt for the TextBox (the focus will remain in the TextBox).
If you use DataSourceUpdateMode.OnPropertyChanged, it will update the data source with every keystroke or text change.

Can I force the CustomUnboundColumnData event to be raised?

I have a usercontrol that has custom unbound data columns in a gridView. I want to retain view state when switching between screens in my app in terms of selected tabs and focused rows etc.
When I do this switch and restore that view state, my custom data is gone and the CustomUnboundColumnData event handler does not get used at all.
Is there a way to raise the CustomUnboundColumnData event for this gridView after I restore my view state?
Call the RefreshRow or RefreshRowCell methods, passing a row handle and optionally a column.
If you are using the ASPxGridView, I would suggest that you turn off the EnableRowsCache property. One more solution is to call the ASPxGridView's DataBind method explicitly within the Page_Init method.
Found a work around. I was refreshing my data in this grid view anyway. However, the few columns that had unbound data needed to be re-added to the grid before refreshing this data.

In C# (winforms) how to synchronize a textbox and datagridview so changes in one show up in the other

No database involved here, I am creating my own datatable in code, and binding it to a textbox and a datagridview.
When I change current record in the grid the textbox updates to the current value.
But what's the best way to synchronise changes between the values in the textbox and the datagrid. If I change one then it doesn't change the other unless I go to another record and back.
I can do this by adding a datagrid.Refresh() to the textbox Validated event, and presumably something to the CellValidated event on the datagrid, but it seems like I might be going about this the wrong way.
Edit:
Based on the answer below, my question should be: is there a way to notify bound controls of changes in a DataTable they are bound to, or must the code use a BindingList or do it manually instead.
I suggest you use a BindingList<T> rather than a DataTable, where T is your "business object" that represents each record displayed in the grid. Then your business object should implement INotifyPropertyChanged and fire NotifyPropertyChanged whenever the value in the text box changes, either by binding the desired property to TextBox.Text or updating the appropriate property of the selected business object whenever TextBox.TextChanged fires.

Detecting changes to the source data using the XmlDataProvider and a bound TextBox

I have a TreeView bound to an XmlDataProvider. The name of a node is reflected in the tree and the data of the node is in a TextBox. When you click on a tree item, it loads the data in the TextBox.
What is the best way to determine when a user has changed the source data referenced by the XmlDataProvider.
I would like to put an asterisk (*) next to the filename to say that the document is dirty and should be saved. This will also determine whether the user is reminded to save when the exit the program.
Unfortunately, when the TextBox is updated by the two-way binding, it fires the TextChanged event.
Is there a way to determine that the TextChanged event was fired by the two-way binding code instead of by the user?
I also tried to use the DataChanged event on the XmlDataProvider but this event only appears to fire when initially loading the data.
I suppose that I could trap the key presses in the TextBox directly but it seems messy.
[Edit] It appears that PreviewTextInput seems to work for this purpose...Is there a better way?
PreviewTextInput did the trick.

Categories