1

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?

Fredrick Gauss
  • 5,126
  • 1
  • 28
  • 44
  • I'd like to know what problem you're actually trying to solve by not using a BindingSource ? is it just as a matter of digging into the inner workings of the DataBinding mechanism ? – Luc Morin Aug 18 '11 at 20:19

2 Answers2

0

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.

Wowa
  • 1,791
  • 1
  • 14
  • 24
  • ok, but what can be done to achive my purpose? How can we prevent the controls updating their values? Any suggestion? – Fredrick Gauss Aug 17 '11 at 14:14
  • DataTable doesn't have the RaiseListChangedEvent property. You could create your own DataTable and add this property. If the property is true override the OnPropertyChanged methode and stop it. Another way would be to put the values in variables and parse with them. they won't change. while parsing you should Suspend the layout or the user would see all changes. – Wowa Aug 17 '11 at 14:25
  • Besides your advices, i tried DataRowView and made the changed on it, after that just called this.BindingContext[myTable].EndCurrentEdit(); sufficed. Hope it isn't a bad idea. – Fredrick Gauss Aug 17 '11 at 15:14
  • No, this is just fine. in the background the same thing happens as if you save the values to variables and from the variables to the current row. – Wowa Aug 18 '11 at 06:03
0

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.

Fredrick Gauss
  • 5,126
  • 1
  • 28
  • 44