Solved

Trouble with binding source and combo boxes in C#

Posted on 2014-02-04
5
518 Views
Last Modified: 2014-02-05
Hi experts

I need some help on binding source and combo box.
I have two combo boxes and both are bound to the same binding source.

           // There are two classes.
            public class SchoolClass
            {
                   public string ClassName {get;set;}
                   public int Id { get; set;}
                   public BindingList<Student> Students {get;set;}
            }

            public class Student
            {
                    public string Name {get;set;}
                    public int Id { get; set;}
            }

            public class Report
            {
                       int MyClass;
                       int MyStudent;
            }

            // Get a collection of school classes, and report.
            BindList<SchoolClass> classes = GetClasses();
            Report report = GetReport();

            // Create a binding source to bind to binding list.
            BindingSource bsClasses = new BindingSource();
            bsClasses.DataSource = classes;

          // Set up the binding for my two combo boxes.
            cbClass.DataBindings.Add("SelectedIndex", report , "MyClass", true, DataSourceUpdateMode.OnPropertyChanged, -1);
            cbClass.DisplayMember = "ClassName";
            cbClass.ValueMember = "Id";
            cbClass.DataSource = bsClasses;

            cbStudent.DataBindings.Add("SelectedIndex", report , "MyStudent", true, DataSourceUpdateMode.OnPropertyChanged, -1);
            cbStudent.DisplayMember = "Students.Name";
            cbStudent.ValueMember = "Students.Id";
            cbStudent.DataSource = bsClasses;

Open in new window


The advantage behind was when user selects a school class in cbClass, then cbStudent will automatically be populated by all students of the selected school class.

My trouble is when 'report' is later updated, change in 'cbClass' will automatically reset the 'SelectedIndex' in cbStudent to zero . But what I want is the 'SelectedIndex' in cbStudent to point to the 'MyStudent' in 'report'.

Could someone point to me what I had done wrong. Thanks.
0
Comment
Question by:dominicwong
  • 3
5 Comments
 
LVL 39

Accepted Solution

by:
Kyle Abrahams earned 250 total points
ID: 39836641
I would add a selectedIndexChanged eventHandler to do that:

protected void cbClass_selectedIndexChanged(object sender, eventargs e)
{
    cbStudent.SelectedValue = report.MyStudent.ToString();
}
0
 
LVL 13

Assisted Solution

by:Ashok
Ashok earned 250 total points
ID: 39837205
Try this.....

cbStudent.SelectedValue = ((Report)(bsClasses).CurrentItem).MyStudent;
0
 

Author Comment

by:dominicwong
ID: 39837500
Thanks Kyle and Ashok.

I'd tried as Kyle suggested.
While "report.MyStudent.ToString()" contains a valid value, setting " cbStudent.SelectedValue" didn't take any effect at all. The value of "cbStudent.SelectedValue " immediately after the setting still has value of "null". Don't know why??

Event handler "cbStudent_SelectedValueChanged" was called immediately after "cbClass_SelectedValueChanged". Inside the event handler "cbStudent_SelectedValueChanged", "cbStudent.SelectedValue" somehow (don't know how) has the value of the first element in the list (which is wrong).

I'd tried as Ashok suggested.
"bsClasses" has DataSource set to "BindList<SchoolClass>". Its 'CurrentItem' is of type 'SchoolClass', and can't be casted to type 'Report'. It threw an exception.

Anyway, I think the end result will still be the same. Somehow,  "cbStudent.SelectedValue" doesn't take any setting inside the event handlers "cbClass_selectedIndexChanged" or "cbClass_selectedValueChanged".
0
 

Author Comment

by:dominicwong
ID: 39837582
I managed to resolve the issue by setting the "cbClass.SelectedValue" and "cbStudent.SelectedValue" with the values from "report" on another method. As a result, when the form is brought up, the two combo boxes were already synchronized with binding source, and cbStudent will not be reset due to change in cbClass.

Thanks for your help.
0
 

Author Closing Comment

by:dominicwong
ID: 39837584
Thanks again.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now