.NET Forum / Windows Forms / WinForm Data Binding / October 2004
Change databound property from code and force field update
|
|
Thread rating:  |
WiZZiX - 08 Oct 2004 14:21 GMT I have 2 checkbox controls on a form. Both are bound to a different field in one underlying datatable with their 'checked' properties .
When you click the first checkbox I the CheckedChanged event is triggered in which I set the checked property of the second control equal to that of the first.
Althought the visible part is chenged (I see the new valu e on the form), it is not written to the proposed value of the field in the datatable. How can I force this filed update without having to send focus to and from the other checkbox?
Any suggestions arre much appreciated,
WiZZiX
"Jeffrey Tan[MSFT]" - 11 Oct 2004 07:12 GMT Hi WiZZiX,
Based on my understanding, you programmatically change a bound control's value then find the change did not reflect into the underlying datasouce.
For this issue, we have 2 options: 1. Explicitly changes the bound control's focus to force the databinding, like this. 2. Invoke the EndCurrentEdit method on the bindingmanager for the CheckBox. I suggest you use this way.
Sample code snippet below: DataTable dt; private void button1_Click(object sender, System.EventArgs e) { dt=new DataTable(); dt.Columns.Add(new DataColumn("prop1", typeof(bool))); dt.Columns.Add(new DataColumn("prop2", typeof(bool))); dt.Columns[0].DefaultValue=false; dt.Columns[0].DefaultValue=true;
for(int i=0;i<3;i++) { DataRow dr=dt.NewRow(); dr[0]=false; dr[1]=true; dt.Rows.Add(dr); } this.checkBox1.DataBindings.Add (new Binding("Checked", dt, "prop1")); this.checkBox2.DataBindings.Add (new Binding("Checked", dt, "prop2")); }
private void checkBox1_CheckedChanged(object sender, System.EventArgs e) { this.checkBox2.Checked=this.checkBox1.Checked; this.checkBox2.DataBindings[0].BindingManagerBase.EndCurrentEdit(); // this.checkBox2.Focus(); //this is another option // this.checkBox1.Focus(); }
private void button2_Click(object sender, System.EventArgs e) { MessageBox.Show(dt.Rows[0][1].ToString()); } ============================================================== Please apply my suggestion above and let me know if it helps resolve your problem.
Thank you for your patience and cooperation. If you have any questions or concerns, please feel free to post it in the group. I am standing by to be of assistance.
Best regards, Jeffrey Tan Microsoft Online Partner Support
 Signature Get Secure! - www.microsoft.com/security This posting is provided "as is" with no warranties and confers no rights.
WiZZiX - 11 Oct 2004 08:57 GMT Hello Jeffrey,
Thanks for your reply. In my search for a solution I thought about trying your first suggestion but it seems a bit 'ugly'.
Your second one is almost perfect. Unfortunately it has to drawbacks. First of all it sends the data to the current data not to the proposed data. Secondly it will send a 'refresh' to all other bound controls, including the first checkbox that started the whole thing. Since the first checkbox is in its CheckedChanged event it regets its old value from the dataset and the whole change is in a way reset. If I use the Validated event I have to wait until I leave the first checkbox in order to see results.
I hope you can shine some more light on the issue.
With regards, WiZZiX
> Hi WiZZiX, > [quoted text clipped - 53 lines] > Get Secure! - www.microsoft.com/security > This posting is provided "as is" with no warranties and confers no rights. "Jeffrey Tan[MSFT]" - 12 Oct 2004 07:47 GMT Hi WiZZiX,
Thanks very much for your reply!
Yes, I see your concern, the second solution may have some problem, because sometimes when we change checkbox1's checked property, it did not lose focus, so the databinding did not occur, but in checkBox1_CheckedChanged, we changed checkBox2.Checked property with EndCurrentEdit, which force checkbox2's databinding occurs, then the old value in datatable will reflect up to the checkbox1, which overlayed the new value of checkbox1.
For this issue, we can not call this.checkBox1.DataBindings[0].BindingManagerBase.EndCurrentEdit() in checkBox1_CheckedChanged to force the checkbox1's new value to be updated to datatable, because the before checkBox1_CheckedChanged is completed, the EndCurrentEdit() will not successfully update the datatable.
I think the only solution for us is using the first way I provided explicitly change checkbox1 and checkbox2's focus to force 2 checkboxes' databinding. =============================================== Thank you for your patience and cooperation. If you have any questions or concerns, please feel free to post it in the group. I am standing by to be of assistance.
Best regards, Jeffrey Tan Microsoft Online Partner Support
 Signature Get Secure! - www.microsoft.com/security This posting is provided "as is" with no warranties and confers no rights.
"Jeffrey Tan[MSFT]" - 15 Oct 2004 03:32 GMT Hi WiZZiX,
Does my reply make sense to you? Do you still have concern on this issue? Please feel free to tell me, Thanks
Best regards, Jeffrey Tan Microsoft Online Partner Support
 Signature Get Secure! - www.microsoft.com/security This posting is provided "as is" with no warranties and confers no rights.
WiZZiX - 18 Oct 2004 08:43 GMT Hello Jeffrey,
thanks for addressing my problem. I was busy busy last week, so had to let go of the issue for a moment.
I see yous solution and have seen it work, but it seems like a 'dirty' way of getting things done that seem so arbitrary. However, I have found to other possible solutions and hope you can comment on them:
1. After changing the value of checkbox2 I call a SuspendDataBinding followed by ResumeDataBinding. This seems to send the new value of checkbox2 to the dataset.
2. I have thought about turning things around and changing the value of Checkbox2 in the dataset from the Checkbox1_Changed event. The problem now is to get the dataset value to the checkbox2. Using the refresh method of the CM won;t work though because it will also refresh checkbox1. An ideas on this one?
With regards, WiZZiX
> Hi WiZZiX, > [quoted text clipped - 6 lines] > Get Secure! - www.microsoft.com/security > This posting is provided "as is" with no warranties and confers no rights. "Jeffrey Tan[MSFT]" - 19 Oct 2004 07:58 GMT Hi WiZZiX,
Thanks very much for your feedback!
I have viewed your 2 ideas:
#1. I am not sure how you get this work. I have tried your idea, but it will end in an infinite loop. Code like this: private void checkBox1_CheckedChanged(object sender, System.EventArgs e) { this.checkBox2.Checked=this.checkBox1.Checked; this.checkBox2.DataBindings[0].BindingManagerBase.SuspendBinding(); this.checkBox2.DataBindings[0].BindingManagerBase.ResumeBinding(); }
#2. After changing the underlying datasource, the changes will reflect to the UI as soon as possible. There is no need to explicitly force the re-binding. But this will end in the same problem with EndCurrentEdit() way. The checkbox1's value will be rebound again. So this option also does not work.
I think my workaround of using focus way only looks "dirty", but it really makes sense. Actually, there are may places such special way is a solid solution.
Thank you for your patience and cooperation. If you have any questions or concerns, please feel free to post it in the group. I am standing by to be of assistance.
Best regards, Jeffrey Tan Microsoft Online Partner Support
 Signature Get Secure! - www.microsoft.com/security This posting is provided "as is" with no warranties and confers no rights.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|