Combo box ” One-to-many ” relation :

Consider this window :
Here we want to add more then one student in one department.
Create a class named Student to get student information :
class Student
{
private string studentName;
  public Student(string studentName) //Constructor
{
this.studentName = studentName;
}
 public string StudentName
{
get { return studentName; }
}
}
Create another class named Department to get department information  :

class Department
{
private string departmentName;
private List<Student> studentList=new List<Student>();

public Department(string departmentName) //Constructor
{
this.departmentName = departmentName;
}
public string DepartmentName
{
get { return departmentName; }
}

     public string AddStudent(Student studentObj)
{
studentList.Add(studentObj);
return “Student has been saved in dept.”;
}
public List<Student> GetStudentList()
{
return studentList;
}
}
Create another class named University to add students in a department  :
class University
{
public string AddStudent(Student studenObject,Department departmentObject)
{
return departmentObject.AddStudent(studenObject);
}
}
NOW DOUBLE CLICK ON ADD BUTTON AND WRITE THIS CODE :

 private void addButton_Click(object sender, EventArgs e)
{
Department deptObject = new Department(deptNameTextBox.Text);

deptSelectComboBox.DisplayMember = “DepartmentName”;
showComboBox.DisplayMember = “DepartmentName”;

deptSelectComboBox.Items.Add(deptObject);
showComboBox.Items.Add(deptObject);
MessageBox.Show(“Department has been added”);
}

After writing this code we can add department name.Just write down the department name in deptNameTextBox and click Add button.Let the department name is CSE.
NOW DOUBLE CLICK ON SAVE BUTTON AND WRITE THIS CODE:

 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);
}

Now we can add student name in a department name.Let the student name is Rajib and chose his department from combo box which is CSE and click Save button.
Now we can see the student name under CSE department just do double click on Show button and write this code :

private void showButton_Click(object sender, EventArgs e)
{
Department selectedDepartment = (Department)showComboBox.SelectedItem;

string detailsMsg = ”  “;  //declare a null string
string deptMessage = “Department Name :t “+selectedDepartment.DepartmentName +”nnStudent Name “;

detailsMsg = detailsMsg + deptMessage;
foreach (Student studentObject in selectedDepartment.GetStudentList())
{
string studentMsg =  “ntt”+ studentObject.StudentName;
detailsMsg = detailsMsg + studentMsg;
}
MessageBox.Show(detailsMsg);

}

Now after clicking Show button we can see :

 

 

Now we can add more than one student in a department just like this.Let,
Now click on Show button and see :

Leave a Reply