public AddOrUpdateDepartment()
        {
            InitializeComponent();

            // register the event handlers
            this.Load += AddorUpdateDepartmentForm_Load;
            buttonAddDepartment.Click    += ButtonAddDepartment_Click;
            buttonUpdateDepartment.Click += ButtonUpdateDepartment_Click;

            // register event handler for when a car is selected
            listBoxAddOrUpdateDepartment.SelectedIndexChanged += (s, e) => GetDepartment();

            // always dispose of the context when the form is closed.
            this.FormClosed += (s, e) => context.Dispose();
        }
Beispiel #2
0
        public AddOrUpdateCourse()
        {
            InitializeComponent();

            // register the event handlers
            this.Load                += AddOrUpdateCourseForm_Load;
            buttonAddCourse.Click    += ButtonAddCourse_Click;
            buttonUpdateCourse.Click += ButtonUpdateCourse_Click;

            // register event handler for when a Course is selected
            listBoxCourses.SelectedIndexChanged += (s, e) => GetCourses();

            // always dispose of the context when the form is closed.
            this.FormClosed += (s, e) => context.Dispose();
        }
        /// <summary>
        /// Remove selected Row
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonDrop_Click(object sender, EventArgs e)
        {
            /* foreach (DataGridViewRow row in dataGridViewRegistration.SelectedRows)
             * {
             *   dataGridViewRegistration.Rows.RemoveAt(row.Index);
             * }*/

            StudentRegistrationEntities context = new StudentRegistrationEntities();

            context.Students.Load();
            context.Courses.Load();
            context.Database.Log = (s => Debug.Write(s));
            context.SaveChanges();

            // get the students and courses, and include nav properties
            var students = context.Students.Include("Courses").ToList();
            var courses  = context.Courses.Include("Students").ToList();

            foreach (DataGridViewRow row in dataGridViewRegistration.SelectedRows)
            {
                StudentCourseRegistration registration = row.DataBoundItem as StudentCourseRegistration;

                // find the student in the db
                Student student = students.Find(s => s.StudentId == registration.StudentID);

                // find the course in teh db
                Course course = courses.Find(c => c.CourseNumber == registration.CourseNumber && c.DepartmentId == registration.course.DepartmentId);

                student.Courses.Remove(course);
            }

            context.SaveChanges();
            context.Dispose();
            UpdateRegistration();
        }
        /// <summary>
        /// Register selected student and course
        /// and add to the registration table
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonRegister_Click(object sender, EventArgs e)
        {
            StudentRegistrationEntities context = new StudentRegistrationEntities();

            context.Students.Load();
            context.Courses.Load();
            context.Database.Log = (s => Debug.Write(s));
            context.SaveChanges();

            var students = context.Students.Include("Courses").ToList();
            var courses  = context.Courses.Include("Students").ToList();

            // check if bothe student and course are selected
            if (dataGridViewStudent.SelectedRows.Count == 0 || dataGridViewCourse.SelectedRows.Count == 0)
            {
                MessageBox.Show("No students or Courses selected");
                return;
            }

            // get the selected students and keep in a list

            List <Student> studentsToRegister = new List <Student>();

            foreach (DataGridViewRow row in dataGridViewStudent.SelectedRows)
            {
                Student student = row.DataBoundItem as Student;
                studentsToRegister.Add(students.Find(s => s.StudentId == student.StudentId));
            }


            List <Course> courseToRegister = new List <Course>();

            foreach (DataGridViewRow row in dataGridViewCourse.SelectedRows)
            {
                Course course = row.DataBoundItem as Course;
                courseToRegister.Add(courses.Find(c => c.CourseId == course.CourseId && c.DepartmentId == course.DepartmentId));
            }

            foreach (Course c in courseToRegister)
            {
                foreach (Student s in studentsToRegister)
                {
                    c.Students.Add(s);
                }
            }

            context.SaveChanges();

            UpdateRegistration();
            context.Dispose();
        }