BindingContext Question

  • Thread starter Thread starter Trecius
  • Start date Start date
T

Trecius

Hello, Newsgroupians:

I've another question regarding databinding. I have a TabControl with two
TabPages. Also -- for simplicity -- I have a ListBox and a TextBox in EACH
page.

I also have a List<Person> that I am using to populate the pages.

When the form loads, I populate each page...

this.Page1.BindingContext = new BindingContext();
this.ListBox1.DataSource = lstPersons;
this.TextBox1.DataBindings.Add("Text", lstPersons, "Name");

this.Page2.BindingContext = new BindingContext();
this.ListBox2.DataSource = lstPersons;
this.TextBox2.DataBindings.Add("Text", lstPersons, "Name");

You can note in the code that I gave each TabPage its own BindingContext, so
they don't share a common index; that is, if the selected item in one ListBox
changes, it won't reflect on the other page. Well, I run this code, and only
the FIRST PAGE changes with respect to the currently selected item in the
ListBox. The second page populates ListBox2, but when I select an item, it
does not reflect my selection in TextBox2.

However, if I do allow both pages to share the same BindingContext, when I
select an item in either ListBox, the textboxes change, but, again, if I
change the selected item on one page, it is reflected on the other page.

Any ideas what I should do? Thank you in advance.


Trecius
 
Hi Trecius,

Not sure what happens with the BindingContext but if the goal is to separate
selected items from the list, you can wrap the list in separate BindingSource
objects and databind against those instead.

BindingSource source1 = new BindingSource();
source1.DataSource = lstPersons;
this.listBox1.DataSource = source1;
this.textBox1.DataBindings.Add("Text", source1, "Name");

BindingSource source2 = new BindingSource();
source2.DataSource = lstPersons;
this.listBox2.DataSource = source2;
this.textBox2.DataBindings.Add("Text", source2, "Name");
 
Hi Trecius,

Not sure what happens with the BindingContext but if the goal is to separate
selected items from the list, you can wrap the list in separate BindingSource
objects and databind against those instead.

BindingSource source1 = new BindingSource();
source1.DataSource = lstPersons;
this.listBox1.DataSource = source1;
this.textBox1.DataBindings.Add("Text", source1, "Name");

BindingSource source2 = new BindingSource();
source2.DataSource = lstPersons;
this.listBox2.DataSource = source2;
this.textBox2.DataBindings.Add("Text", source2, "Name");
 
Mr. Wennevik:

Thank you once again for such an insightful post.


Trecius

Morten Wennevik said:
Hi Trecius,

Not sure what happens with the BindingContext but if the goal is to separate
selected items from the list, you can wrap the list in separate BindingSource
objects and databind against those instead.

BindingSource source1 = new BindingSource();
source1.DataSource = lstPersons;
this.listBox1.DataSource = source1;
this.textBox1.DataBindings.Add("Text", source1, "Name");

BindingSource source2 = new BindingSource();
source2.DataSource = lstPersons;
this.listBox2.DataSource = source2;
this.textBox2.DataBindings.Add("Text", source2, "Name");


--
Happy Coding!
Morten Wennevik [C# MVP]


Trecius said:
Hello, Newsgroupians:

I've another question regarding databinding. I have a TabControl with two
TabPages. Also -- for simplicity -- I have a ListBox and a TextBox in EACH
page.

I also have a List<Person> that I am using to populate the pages.

When the form loads, I populate each page...

this.Page1.BindingContext = new BindingContext();
this.ListBox1.DataSource = lstPersons;
this.TextBox1.DataBindings.Add("Text", lstPersons, "Name");

this.Page2.BindingContext = new BindingContext();
this.ListBox2.DataSource = lstPersons;
this.TextBox2.DataBindings.Add("Text", lstPersons, "Name");

You can note in the code that I gave each TabPage its own BindingContext, so
they don't share a common index; that is, if the selected item in one ListBox
changes, it won't reflect on the other page. Well, I run this code, and only
the FIRST PAGE changes with respect to the currently selected item in the
ListBox. The second page populates ListBox2, but when I select an item, it
does not reflect my selection in TextBox2.

However, if I do allow both pages to share the same BindingContext, when I
select an item in either ListBox, the textboxes change, but, again, if I
change the selected item on one page, it is reflected on the other page.

Any ideas what I should do? Thank you in advance.


Trecius
 
Mr. Wennevik:

Thank you once again for such an insightful post.


Trecius

Morten Wennevik said:
Hi Trecius,

Not sure what happens with the BindingContext but if the goal is to separate
selected items from the list, you can wrap the list in separate BindingSource
objects and databind against those instead.

BindingSource source1 = new BindingSource();
source1.DataSource = lstPersons;
this.listBox1.DataSource = source1;
this.textBox1.DataBindings.Add("Text", source1, "Name");

BindingSource source2 = new BindingSource();
source2.DataSource = lstPersons;
this.listBox2.DataSource = source2;
this.textBox2.DataBindings.Add("Text", source2, "Name");


--
Happy Coding!
Morten Wennevik [C# MVP]


Trecius said:
Hello, Newsgroupians:

I've another question regarding databinding. I have a TabControl with two
TabPages. Also -- for simplicity -- I have a ListBox and a TextBox in EACH
page.

I also have a List<Person> that I am using to populate the pages.

When the form loads, I populate each page...

this.Page1.BindingContext = new BindingContext();
this.ListBox1.DataSource = lstPersons;
this.TextBox1.DataBindings.Add("Text", lstPersons, "Name");

this.Page2.BindingContext = new BindingContext();
this.ListBox2.DataSource = lstPersons;
this.TextBox2.DataBindings.Add("Text", lstPersons, "Name");

You can note in the code that I gave each TabPage its own BindingContext, so
they don't share a common index; that is, if the selected item in one ListBox
changes, it won't reflect on the other page. Well, I run this code, and only
the FIRST PAGE changes with respect to the currently selected item in the
ListBox. The second page populates ListBox2, but when I select an item, it
does not reflect my selection in TextBox2.

However, if I do allow both pages to share the same BindingContext, when I
select an item in either ListBox, the textboxes change, but, again, if I
change the selected item on one page, it is reflected on the other page.

Any ideas what I should do? Thank you in advance.


Trecius
 
Back
Top