I have discussed about "One to many" ralation here.Now I learn how to make "Many to many" relation using combo box.
Consider the previous example.In that example I can add more then one student name in one department.But now I can add more then one student name under more then one department and I can see both department's students name individually by selecting department name from combobox.
To make "Many to many" relation most of the code are same like "One to many" relation.
Here the code for Student and Department classes are same,But we have to add same code in University class.
Class "University" code :
class University
{
private List<Department> departmentList = new List<Department>();
public string AddDepartment(Department departmentObj)
{
departmentList.Add(departmentObj);
return "Department has been saved.";
}
public List<Department> GetDepartment()
{
return departmentList;
}
public string AddStudent(Student studenObject,Department departmentObject)
{
return departmentObject.AddStudent(studenObject);
}
}
NOW DOUBLE CLICK ON ADD BUTTON AND WRITE THIS CODE :
//Now do right click on deptObject in "One to many" relation of ADD BUTTON then click Refactor > Introduce field and then see this code below and compare.
private void addButton_Click(object sender, EventArgs e)
{
deptObject = new Department(deptNameTextBox.Text);
universityObject.AddDepartment(deptObject);
deptSelectComboBox.DataSource = null;
showComboBox.DataSource = null;
deptSelectComboBox.DisplayMember = "DepartmentName";
showComboBox.DisplayMember = "DepartmentName";
deptSelectComboBox.DataSource = universityObject.GetDepartment();
showComboBox.DataSource = universityObject.GetDepartment();
MessageBox.Show("Department has been added");
}
NOW DOUBLE CLICK ON SAVE BUTTON AND WRITE THIS CODE:
//This code is same just delete University universityObj=new University() this.
private void saveButton_Click(object sender, EventArgs e)
{
Student studentObj=new Student(studentTextBox.Text);
Department selectedDepartment = (Department)deptSelectComboBox.SelectedItem;
// University universityObj=new University();
string msg=universityObj.AddStudent(studentObj, selectedDepartment);
MessageBox.Show(msg);
}
{
Student studentObj=new Student(studentTextBox.Text);
Department selectedDepartment = (Department)deptSelectComboBox.SelectedItem;
// University universityObj=new University();
string msg=universityObj.AddStudent(studentObj, selectedDepartment);
MessageBox.Show(msg);
}
NOW DOUBLE CLICK ON SHOW BUTTON AND WRITE THIS CODE :
private void showButton_Click(object sender, EventArgs e)
{
Department selectedDepartment = (Department)showComboBox.SelectedItem;
List<Student> studentList = selectedDepartment.GetStudentList();
string detailsMsg = "";
string deptMessage = "Department Name :\t "+selectedDepartment.DepartmentName +"\n\nStudent Name: ";
detailsMsg = detailsMsg + deptMessage;
foreach (Student studentObject in studentList)
{
string studentMsg = "\n\t\t"+ studentObject.StudentName;
detailsMsg = detailsMsg + studentMsg;
}
MessageBox.Show(detailsMsg);
}
OUTPUT :
I add two departments :
Now I add two students name under CSE department and add two students name under EEE department.
Now select CSE department from showComboBox and click Show button then we can see :
Now select EEE department from showCombobox and click Show button then we will see :
0 Comments