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
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;
}
}
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.