Programming Entity Framework
Programming Entity Framework By Julia Lerman
February 2009
Pages: 828


Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
Chapter 8 - Winforms Binding Problem
dan_p_
post Nov 1 2009, 12:29 AM
Post #1


New Member
*

Group: Members
Posts: 2
Joined: 31-October 09
Member No.: 20,818



Hi Julia,

First of all thank you for providing us a very good book on a promising new subject-Entity Framework. I really enjoy reading it.

While reading chapter 8 first part on WinForms application everything works fine with your sample code. However when I tried to bind all four combo boxes in code only:

private void FillCombos()
{
PrimaryActivityComboBox.DisplayMember = "ActivityName";
PrimaryActivityComboBox.ValueMember = "ActivityID";
PrimaryActivityComboBox.DataSource = activities;
//SelectedItem is handy for working with related data
//this identifies an entire entity, not just an ID
PrimaryActivityComboBox.DataBindings.Add(new Binding("SelectedItem", customerBindingSource,
"PrimaryActivity", true));

PrimaryDestinationComboBox.DisplayMember = "DestinationName";
PrimaryDestinationComboBox.ValueMember = "DestinationID";
PrimaryDestinationComboBox.DataSource = destinations;
PrimaryDestinationComboBox.DataBindings.Add(new Binding("SelectedItem", customerBindingSource,
"PrimaryDestination", true));

SecondaryActivity.DisplayMember = "ActivityName";
SecondaryActivity.ValueMember = "ActivityID";
SecondaryActivity.DataSource = activities;
SecondaryActivity.DataBindings.Add(new Binding("SelectedItem", customerBindingSource,
"SecondaryActivity", true));

SecondaryDestination.DisplayMember = "DestinationName";
SecondaryDestination.ValueMember = "DestinationID";
SecondaryDestination.DataSource = destinations;
SecondaryDestination.DataBindings.Add(new Binding("SelectedItem", customerBindingSource,
"SecondaryDestination", true));
}


and cleared all binding properties in the designer, things started to work wrong. The PrimaryActivity and PrimaryDestination combos are showing the correct values but the SecondaryActivity and SecondaryDestination controls shows exactly the same values as their primary counterparts.
The same behaviour when binding all four combos in designer only and commenting out the content of FillCombos method.

What can be the cause of this?

Thanks,
Dan
Go to the top of the page
 
+Quote Post
JulieLerman
post Nov 1 2009, 06:17 AM
Post #2


Active Member
****

Group: O'Reilly Author
Posts: 102
Joined: 17-September 08
Member No.: 836



Hi Dan
Winforms databinding still mystifes me.
Howeever it looks like what's happenign is this:

YOu have two comboBoxes pointing to the same datasource. When one repositions the pointer in the datasource, the second one picks that up. THis is a common problem with using datasets/datatables/dataviews also.

I'm pretty sure somewhere in the book (maybe in the later WinForms chapter) I beat this problem by creating a second version of the data source.

e.g.
combobox 1 is tied to activities.
Then I do a linq to objects query of activites to spit out a 2nd copy of activities and bind combobox2 to that.

hth

julie
Go to the top of the page
 
+Quote Post
dan_p_
post Nov 1 2009, 11:23 PM
Post #3


New Member
*

Group: Members
Posts: 2
Joined: 31-October 09
Member No.: 20,818



I've found a workaround on this based on your suggestion. I doubled the activities and destinations collections in code:

1) In form declaration:

private bool _adding;
private List<Activity> activities;
private List<Activity> activities2;
private BAEntities context;
private List<Destination> destinations;
private List<Destination> destinations2;

2) in From1_Load:

activities = context.Activities.OrderBy(a => a.ActivityName).ToList();
destinations = context.Destinations.OrderBy(d => d.DestinationName).ToList();

activities2 = context.Activities.OrderBy(a => a.ActivityName).ToList();
destinations2 = context.Destinations.OrderBy(d => d.DestinationName).ToList();


3) in the code for FillCombos:

private void FillCombos()
{
PrimaryActivityComboBox.DisplayMember = "ActivityName";
PrimaryActivityComboBox.ValueMember = "ActivityID";
PrimaryActivityComboBox.DataSource = activities;
//SelectedItem is handy for working with related data
//this identifies an entire entity, not just an ID
PrimaryActivityComboBox.DataBindings.Add(new Binding("SelectedItem", customerBindingSource,
"PrimaryActivity", true));

PrimaryDestinationComboBox.DisplayMember = "DestinationName";
PrimaryDestinationComboBox.ValueMember = "DestinationID";
PrimaryDestinationComboBox.DataSource = destinations;
PrimaryDestinationComboBox.DataBindings.Add(new Binding("SelectedItem", customerBindingSource,
"PrimaryDestination", true));

SecondaryActivity.DisplayMember = "ActivityName";
SecondaryActivity.ValueMember = "ActivityID";
SecondaryActivity.DataSource = activities2;
SecondaryActivity.DataBindings.Add(new Binding("SelectedItem", customerBindingSource,
"SecondaryActivity", true));

SecondaryDestination.DisplayMember = "DestinationName";
SecondaryDestination.ValueMember = "DestinationID";
SecondaryDestination.DataSource = destinations2;
SecondaryDestination.DataBindings.Add(new Binding("SelectedItem", customerBindingSource,
"SecondaryDestination", true));
}

It is ugly and inefficient and I don't want to think of a scenario with a large form with a lot more controls but it works.
Go to the top of the page
 
+Quote Post
JulieLerman
post Nov 2 2009, 03:50 AM
Post #4


Active Member
****

Group: O'Reilly Author
Posts: 102
Joined: 17-September 08
Member No.: 836



Four points:

1) To be fair, this is not an Entity Framework issue. You need to do the same type of thing with datasets/etc.

2) Your 2nd list is hitting the database again. I'd recommend doing a linq to objects query against the lists that you already have in memory.

activities2 = activities.OrderBy(a => a.ActivityName).ToList();
destinations2 = destinations.OrderBy(d => d.DestinationName).ToList();

3) Yes, lots of ugly code.
You could have a method that will do that for you.

Something like

List<T> DuplicateList<T>(List<T> listtoDuplicate)
{return (from item in listtoDuplicate return item).ToList();}

That's off the top of my head with no intellisense..

You still have to make the extra call, but it's a little cleaner

4) In one of my solutions (chapter 19 again?) I use a method for populating a combobox. That way you don't have to have ALL of that code in FillCombos. You could just call the method 4 times and pass in some parameters.

hth
julie
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

RSS Lo-Fi Version Time is now: 22nd November 2009 - 05:02 AM