I have a fairly routine setup with some check boxes and numeric up/downs bound to a data class through a BindingSource. The default behaviour seems to be that nothing is updated or validated until the control loses focus, and for this application that's useless. Is there some way to make it such that every single blessed control on that form validates and updates immediately after any change, instead of on focus loss?
In Advance Databindings properties of your control, Go to on binding property(like Text for TextBox) and on (Data Source Update Model)combo, Select OnPropertyChange instead of OnValidation.
Related
I'm using the Extended WPF ToolKit's DecimalUpDown control (v1.7). The control behaves as I expect when using the spinner controls but not when text is edited directly. This is a basic MVVM WPF app with the control bound to a View Model decimal property named CurrentWidth.
In the ViewModel there are various validation rules being enforced for CurrentWidth, at the end of the property it does a RaisePropertyChange("CurrentWidth"); sometimes leaving CurrentWidth unchanged if the value doesn't validate.
All the error checking, value reverting etc works when change is made with the spinner controls. When the user enters text directly in the box, the validation still works but the box is left showing what the user entered. When I send out some debug info, both the Value property and Text properties have the correct unmodified value but the box still shows the user entered value. I tried adding a LostFocus event handler and called InvalidateVisual() on the sender control and even tried an UpdateLayout() as well but after tabbing off the control, the user entered text still shows. Anyone know how to get it to reflect the actual current value?
Download WPFToolkit 1.8.0. This bug is solved there.
I'm pretty new to c# and Winforms and I'm wondering what is the best approach to the following screen design.
I have a window that contain a Datagrid wich would be read-only. Beneath the grid, I have the detail of the records in differents fields (textbox, combobox, checkbox).
What I want is that when the user click on an item in the datagrid, the data will be shown in the detail fields.
That part is pretty easy, but I want to be able to update the fields automatically, wich means, I would prefer to not have to press a Save button.
Let's say that I click an item in the datagrid, change some value in the detail fields and the I click on another item in the datagrid, then I also want to perform some validation and calculation before the record get updated.
What I was thinking at first was to get the button for "new", "edit", "save" action and lock and unlock the fields accordingly and keep a flag to know if i need to insert or update the data, but then I realized that I would prefer to not have thoses button and have the save performed automatically.
Is there any sample somewhere that does what I want?
Also, would you guys using the built-in databinding functunality or just use a dataset object in code?
Pretty common scenario.
On selected row change of grid you know which datarow you shoul bind to the other controls. when same event happens again you validate and save or cancel in case of errors.
You can make use of DataGridView.CellEndEdit Event to get the new value and DataGridView.CellBeginEdit event to get the old value and update your data if there is any change
In a form, I have a TextBox Binding an Object on its member property "Title". Along with it is a "Save" button to test the binding.
Seems like the underlying object property does not get updated unless the textbox loses focus. But there no form.ActiveControl.Blur() for use. Besides, this does not seem like a sound hack.
Anyway to do this better? Thanks.
EDIT: Sorry for not being clear. My question is in the title: "How to commit a TextBox". I use the term "commit" from the DataGridView commit and BindingSource commit. And it's in WinForms. (Have never worked with WPF, so it didn't occur to me. Sorry).
The actual scenario I have is I have a bunch of TextBox binded to property of a single Object. The user enters values in all the TextBox and when the user clicks save (toolbar button), the last TextBox is still in focus (or in editing mode) hence the save will not capture the last value in the last textbox.
I want to find the correct way to "commit" the textbox value just before saving.
Thanks.
Since the question has been updated to indicate this is WinForms, you'll need to handle things a little differently than if this were a WPF application. Fortunately, it turns out that the solution is very simple.
Whenever the user clicks on the "Save" button (so, say, in your Save button's Click event handler), you need to call the EndEdit method on your BindingSource. This will cause all pending changes to be committed to the underlying data source, exactly what you were hoping to accomplish.
Also see the relevant documentation on MSDN for more details.
Sounds like WPF from the problem description..
You want to change the binding so that it updates when the property value changes instead of when the textbox loses focus (which is the default when binding to TextBox.Text). You can do this by setting the UpdateSourceTrigger property on your binding:
<TextBox Text="{Binding UpdateSourceTrigger=PropertyChanged}"/>
I use data binding to display values in text boxes in a C# Windows Forms client. When the user clicks Save, I persist my changes to the database. However, the new value in the active editor is ignored (the previous value is saved). If I tab out of the active editor, and then Save, the new value is persisted, as expected.
Is there a way to force the active control to accept its value before persisting?
If you can get the Binding instance that corresponds to the input (the TextBox), you can call the WriteValue method to force the value from the control to the object it is bound to.
Also, you can call the EndCurrentEdit method on the BindingManagerBase class (usually a CurrencyManager class instance) to finish the edit, but that requires implementation of the ICancelAddNew or IEditableObject interface on the object that is bound to (and wouldn't require you to fish for the binding).
The solution I've used is to call ValidateChildren on the Form from the Save event (call), before actually saving the database records. This forces validation of all fields and thus binding to occur without losing focus of the control currently being edited on the form. It is real handy if the save button is on the Windows menu system and not form itself - plus it returns False if data in any control on the form is invalid and thus can be used to prevent saving errant data.
This also gets around inconsistent updating of the bound field that occurs when OnPropertyChanged is used as a binding method instead of OnValidation. Also, it is critical if the binding method is set to Never with separate WriteValue calls made for each validated event trapped per control.
This is kind of a hack, but try setting the focus away from the active editor (by setting the focus to something else, like the save button for example) in the button event before you call save.
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.