dominicwong
asked on
Trouble with binding source and combo boxes in C#
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.
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.
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;
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
Thanks for your help.
ASKER
Thanks again.
ASKER
I'd tried as Kyle suggested.
While "report.MyStudent.ToString
Event handler "cbStudent_SelectedValueCh
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_selectedIndexChan