Groups | Blog | Home
all groups > dotnet windows forms databinding > august 2004 >

dotnet windows forms databinding : How to make the binding active?


Thomas Hansen
8/18/2004 11:00:05 AM
I have a form with a TabControl with two TabPages and a TextBox control =
on each TabPage. Each of the TextBoxes are bound to a datasource like =
TextBox1.DataBindings.Add("Text", MyObject, "FirstName"). The problem is =
that the binding of the TextBox on the *second* TabPage is not active =
immediately (IsBinding =3D False), it only becomes active once the =
second TabPage is clicked?? This means that if I have a button on the =
form that does a MsgBox(TextBox2.Text) the Text is nothing until I click =
on the second TabPage.

Any help on this question would be appreciated!

Thanks,

lakmann NO[at]SPAM gmx.de
8/20/2004 8:32:12 AM
Is the binding actually inactive (poperty IsBinding == false)?
I think it is a problem with the binding context of the tabpages. Try
giving them the same binding contexts as the parent form before
creating the binding.
IIRC there is a bug, that tabcontrols and other container controls get
binding contexts of their own and don't use the one of the form.

I struggled a lot with databindings. I got best results with:
- moving the generation of the bindings from InitializeComponent into
a method CreateBindings and calling this in the load event of the form
- before CreateBindings I call SyncroniseBindingContexts(this)

public void SyncroniseBindingContexts(Control con) {
if (con.HasChildren) {
foreach (Control subcon in con.Controls) {
if (subcon is Panel || subcon is TabControl || subcon is TabPage
|| subcon is GroupBox || subcon is DataGrid || con is DataGrid) {
subcon.BindingContext = con.BindingContext;
SyncroniseBindingContexts(subcon);
}
}
}
Thomas Hansen
8/23/2004 11:12:16 AM
Hi Frank

Yes, IsBinding = False on controls put on TabPages until TabPage is clicked.
Thanks for sharing your knowledge, I will give it a try...

Thanks,

Thomas

"Frank Lakmann" <lakmann@gmx.de> skrev i en meddelelse
news:1d5cd7b3.0408200732.462f7bfd@posting.google.com...
[quoted text, click to view]

AddThis Social Bookmark Button