I'm binding a property of an object to the "Checked" or "EditValue" of a check box, and the value is "committed" to the object only when the check box control loses the focus. I would like the value to be committed as soon as the user changes the value. How can I accomplish this?
This question is also valid for a drop down, if I bind a value to a drop down, in some case I would like the value to be changed as soon as the user changes the values.
Thanks!
How can I force a binded value to be committed when changing
Answers
Hi Alex,
Thank you for the feedback. In fact this is a MS data binding issue. If you add the data binding after assigning the EditValueChanged event, this event will fire before the control's DataBinding has been completely initialized. Thus it will not work as expected. As a workaround I can suggest that you perform the postponed validation:
private void simpleButton1_Click( object sender, EventArgs e )
{
List<DataClass> list = new List<DataClass>();
list.Add( new DataClass( 1, "aa" ) );
list.Add( new DataClass( 2, "bb" ) );
list.Add( new DataClass( 3, "ss" ) );
list.Add( new DataClass( 4, "ww" ) );
_bo = new BusinessObject( list );
lookUpEdit1.Properties.DataSource = list;
lookUpEdit1.EditValueChanged += new EventHandler(lookUpEdit1_EditValueChanged);
lookUpEdit1.DataBindings.Add(new Binding("EditValue", _bo, "DataID", true));
textEdit1.DataBindings.Add( new Binding( "EditValue", _bo, "DisplayValue", true ) );
}
void lookUpEdit1_EditValueChanged(object sender, EventArgs e)
{
BeginInvoke(new MethodInvoker(PerformValidation));
}
void PerformValidation()
{
lookUpEdit1.DoValidate();
}
Please feel free to contact us if you need any additional assistance in this regard. We will be happy to help you.
Thanks,
Stan.
I know this is 8 year old post but BRILLIANT!
i came across lots of other posts that suggested dovalidate but none suggested that it needed to be invoked, as soon as i did that i started working!
I'm happy to hear that our assistance was helpful!
Feel free to contact us if you have other questions.
Hi Alex,
To implement this functionality, you can handle the CheckEdit's CheckedChanged event as shown below:
private void checkEdit1_CheckedChanged(object sender, System.EventArgs e) {
(sender as DevExpress.XtraEditors.CheckEdit).DoValidate();
}
Thanks,
Stan.
It's working for the checkbox but this solution doesn't work for the checkbox.
Hi Alex,
I'm sorry, but I don't quite understand you. Could you please clarify what you mean by saying "It's working for the checkbox but this solution doesn't work for the checkbox"? If possible, please provide me with a sample project which demonstrates the issue. I'll review it and try to provide you with a solution. If you talking about a drop down editor, you can implement the same feature by handling its EditValueChanged event:
private void <editor>_EditValueChanged(object sender, System.EventArgs e) {
(sender as DevExpress.XtraEditors.BaseEdit).DoValidate();
}
I'm looking forward to hearing from you.
Thanks,
Stan.
Ok, I've just realized that the DoValidate works when the EditValueChanging event is hooked from the designer but it doesn't work when the event is hooked mannually. I've attache a sample application that reproduct the problem.
To see the problem:
1- Add a break point in the setter of the binded lookup edit.
2- Click on the button
3- Change the value of the lookup edit.
The editvaluechanging event is called, but when I call the DoValidate method, the value is not commited to the data source.
Let me know if you need more details.
Hi Alex,
If you take a look at my previous post closely, you will notice that I suggested that you handle the EditValueChanged event but not the EditValueChanging. So, I have corrected your sample based upon my previous suggestion and it works just fine now. Please review it and inform me whether this approach suits your needs.
Thanks,
Stan.
I don't know if it's caused by the version 6.2 but I've noticed that if you hook the event EditValueChanged before setting the datasource, the DoValidate has no effect ( ie the data is not commited to the binded property). If you use the sample that you sent and hook the event add the beginning of the simpleButton1_Click method instead of after setting the datasource, you will notice that the event is triggered and the DoValidate is called but the data is not set to the object.
Clarification : I've noticed that it needs to be hook after the databinding is set, not after the datasource.