开发者

Control binding with CurrencyManager without BindingSource

开发者 https://www.devze.com 2023-03-28 22:08 出处:网络
I decided not to use bindingSource class but implement binding functionality on my windows form application. I succeed to some points but little complication occurs. I would like to find out the reaso

I decided not to use bindingSource class but implement binding functionality on my windows form application. I succeed to some points but little complication occurs. I would like to find out the reason. I have DataTable filled from datasource and three controls: textbox1, textbox2, checkedBox1. I managed to bind the controls and show the values by;

txtBox1.DataBindings.Add("Text", myTable, "NAME", true, DataSourceUpdateMode.Never);

As you guess, myTable is the datatable contains the data filled from database table. I can get the current row values with CurrencyManager, such as;

DataRow dr = ((DataRowView)this.BindingContext[myTable].Current).Row;

But when i want to change the values using current row, somehow after first field set, others automatically changes to prior values. I mean:

dr["NAME"]= textBox1.Text;


dr["SURNAME"] = textBox2.Text;  //this set seems useless

First assignment works but then form controls that textBox2, checkBox1 changes their values back to original before the editing begins. So i can not update the full row in this way.

On the other hand, with using BindingSource i would get the current row similar style:

DataRow drB = ((DataRowView)bindingSource1.Current).Row;

And change the fields of this row by:

drB["NAME"] = textBox1.Text;
drB["SURNAME"] = textBox2.Text;
drB["ACTIVE"]开发者_如何转开发 = checkBox1.Checked;

with ending the edit with:

bindingSource1.EndEdit();

All table is ready to update because whole row changed successfully. I would like to see the difference, and manage it without using bindingSource. Does it mean that I should use something EndEdit() does? What am I missing, or forget the obsession?


After the first change (dr["NAME"]= textBox1.Text;) OnPropertyChanged/OnListChanged is raised from the binding. the second textbox and the checkbox listen to this message and are updating there values too, but these values are the old ones.


Right answer has shown itself, 'cause i missed a little cue. That was using temp variables before changing the datarow field from the control values directly. So even control values changed back to their orignal values, we could again achive to change whole row fields thus control values to the new values eventually.That is :

string s1 = textBox1.Text;
string s2 = textBox2.Text;
bool b1 = checkBox1.Checked;
dr["NAME"] = s1;
dr["SURNAME"] = s2;
dr["ACTIVE"] = b1;

With these temp variable, however the control values changed after first assignment, at the end they all with the new values.
If someone needs, good luck.

0

精彩评论

暂无评论...
验证码 换一张
取 消