KB Article A1110
Visible to All Users

A data-bound editor's value is not saved when data is posted to a persistent database

Description:
I'm using the TextEdit editor bound to data. If I type a value in the text box, it will not update the underlying property, unless I move focus off the control. I have a toolbar button, which saves changes to a persistent database. However, the editor's value is not saved. What should I do to post the editor's value without having to switch focus prior to pressing the Save toolbar button?

Answer:
You should call the editor's DoValidate method to force its value to be saved. XtraEditors call DoValidate themselves when they lose focus. However, if your Save button does not get focus when it is clicked (e.g. a toolbar button), you should explicitly call the DoValidate method of the focused editor. The focused editor can be obtained from the ContainerControl.ActiveControl property. Here is the necessary code:

C#
// post an editor's value Control activeCtrl = ActiveControl; while(activeCtrl is ContainerControl) activeCtrl = ((ContainerControl)activeCtrl).ActiveControl; if(activeCtrl is DevExpress.XtraEditors.TextBoxMaskBox) activeCtrl = activeCtrl.Parent; if(activeCtrl is DevExpress.XtraEditors.BaseEdit) ((DevExpress.XtraEditors.BaseEdit)activeCtrl).DoValidate(); // end editing of the current row BindingContext[dataSet, "Customers"].EndCurrentEdit(); // update a database if(dataSet.HasChanges()) dataAdapter.Update(dataSet.GetChanges());
Visual Basic
' post an editor's value Dim ActiveCtrl As Control = ActiveControl While TypeOf ActiveCtrl Is ContainerControl ActiveCtrl = CType(ActiveCtrl, ContainerControl).ActiveControl End While If TypeOf ActiveCtrl Is DevExpress.XtraEditors.TextBoxMaskBox Then ActiveCtrl = ActiveCtrl.Parent End If If TypeOf ActiveCtrl Is DevExpress.XtraEditors.BaseEdit Then CType(ActiveCtrl, DevExpress.XtraEditors.BaseEdit).DoValidate() End If ' end editing of the current row BindingContext(dataSet, "Customers").EndCurrentEdit() ' update a database If dataSet.HasChanges() Then dataAdapter.Update(dataSet.GetChanges()) End If
Comments (2)
AD AD
Appy Developers 7 years ago

    As this nagged me for 1 year, sometimes it comes, and sometimes it goes.
    this is a workaround:
    put the field name in the amountPayed03CalcEdit.Tag

    private void frm_Load(object sender, EventArgs e)
            {
              amountPayed03CalcEdit.EditValueChanging +=amountPayed_EditValueChanging;
            }

    void amountPayed_EditValueChanging(object sender, ChangingEventArgs e)
            {
                var value = 0m;
                if (Decimal.TryParse(e.NewValue.nz2("0"), out value))
                {
                    var oRow = _bindingNavigator.getBindingSourceCurrentRow();
                    if (oRow is null)
                        return;

    var editor = (BaseEdit)sender;
                    oRow[editor.Tag.ToString()] = value;
                }
            }

    DevExpress Support Team 7 years ago

      Hello,

      I've created a separate ticket on your behalf (T708603: How to post EditValue to the data source when an end-user inputs a new value). It has been placed in our processing queue and will be answered shortly.

      Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

      Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.