Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / March 2008

Tip: Looking for answers? Try searching our database.

ComboBox DataBinding 1/2 working

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steve K. - 17 Mar 2008 01:16 GMT
I've got a strange situation here with a DataBound ComboBox.  When I change
the selected item from the list my DataBound object is updated.  When I try
to restore the data the item in the ComboBox is not selected.

Here is the relevant code:
<code>
public void SetAvailablePlans(List<InsurancePlan> plans)
{
   //comboBox_PlanType.Items.Clear();
   InsurancePlan blankPlan = new InsurancePlan();
   blankPlan.Name = "New";
   blankPlan.InternalID = "-1";

   plans.Insert(0, blankPlan);

   comboBox_PlanType.SelectedIndexChanged -=
comboBox_PlanType_SelectedIndexChanged;
   comboBox_PlanType.DataSource = plans;
   comboBox_PlanType.DisplayMember = "Name";
   comboBox_PlanType.ValueMember = "InternalID";
   comboBox_PlanType.SelectedIndexChanged +=
comboBox_PlanType_SelectedIndexChanged;
}

private void BindPolicy(InsurancePolicy policy)
{
   if(policy != null)
   {
       //  other controls removed...

       comboBox_PlanType.DataBindings.Add(new Binding("SelectedItem",
policy, "PlanType"));
   }
}
</code>

I'm not using a BindingSource because there are some strange things that
happen when you use a BindingSource with nested UserControls.

Looking at the above code;  Insurancepolicy has an InsurancePlan property
(called "PlanType").  Unless I'm really missing something, this seems to be
the correct way to set things up.

Like I said, the DataBinding is working to an extent, the item based in to
BindPolicy is updated when I change the selected item in the ComboBox, but
it won't SET the selection when it's initially bound.

Driving me nuts... hopefully someone can shed a little light on this for me.

Thanks,
Steve
Morten Wennevik [C# MVP] - 17 Mar 2008 12:17 GMT
Hi Steve,

It's kind of hard to tell from your sample but things to check would be the
order you bind the policy to the control.  Is this done before or after you
set the ComboBox.DataSource?  Try calling BindPolicy after SetAvailablePlans

Using a BindingSource you would typically do a ResetBinding() after having
set everything up to force all controls to update themselves.  By the way, a
BindingSource works fine in nested usercontrols if set up properly.

If you are changing PlanType in code, you may need to implement the
INotifyPropertyChanged interface in your business objects.

Signature

Happy Coding!
Morten Wennevik [C# MVP]

> I've got a strange situation here with a DataBound ComboBox.  When I change
> the selected item from the list my DataBound object is updated.  When I try
[quoted text clipped - 47 lines]
> Thanks,
> Steve

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.