private bool TasksForm_EditEventHandler(object sender) { try { db.SaveChanges(); FillDataGridView(); monthComboBox_SelectedIndexChanged(null, null); return(true); } catch { MessageBox.Show("An error occurred while recording the changes! Please, try again!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } }
private void changeStatusBtn_Click(object sender, EventArgs e) { var result = MessageBox.Show( "Are you sure you want to change the status of the project ?", "Confirm Project Status Change", MessageBoxButtons.YesNo, MessageBoxIcon.Question ); if (result == DialogResult.Yes) { string newStatus = inProgressRadioButton.Checked ? "O" : "C"; this.project.ProjectStatus = newStatus; db.SaveChanges(); this.changeStatusBtn.Enabled = false; MessageBox.Show( $"Project Status was successfully updated!", "Successful Status Update", MessageBoxButtons.OK, MessageBoxIcon.Information ); } }
private void registerEmployeeBtn_Click(object sender, EventArgs e) { string name = nameTextBox.Text; string surname = surnameTextBox.Text; string lastName = lastNameTextBox.Text; string egn = egnTextBox.Text; string position = positionComboBox.Text; DateTime hireDate = hireDateTimePicker.Value; if (IsValidName(name, "Name") && IsValidName(surname, "Surname") && IsValidName(lastName, "Last Name") && IsValidEgn(egn) && IsValidPosition(position)) { try { var employee = new Employee() { EmployeeId = db.Employees.Count() + 1, EmployeeName = name, EmployeeSurname = surname, EmployeeLastname = lastName, EmployeeEgn = egn, EmployeePosition = position, EmployeeHiredate = hireDate }; db.Employees.Add(employee); db.SaveChanges(); MessageBox.Show( $"{employee.EmployeeName} {employee.EmployeeSurname} {employee.EmployeeLastname} was successfully registered!", "Successful Employee Registration", MessageBoxButtons.OK, MessageBoxIcon.Information ); this.Close(); } catch { MessageBox.Show("An error occurred while recording the data! Please, try again!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
private void EmployeeEditForm_EditEventHandler(object sender, EmployeeEditForm.EditEventArgs args) { try { db.SaveChanges(); dataGridView1.Rows.Remove(dataGridView1.Rows[args.RowIndex]); dataGridView1.Rows.Insert(args.RowIndex, args.Employee.ToDataView()); UpdateSets(); MessageBox.Show( $"{args.Employee.EmployeeName} {args.Employee.EmployeeSurname} {args.Employee.EmployeeLastname}'s account was successfully edited!", "Successful Employee Update", MessageBoxButtons.OK, MessageBoxIcon.Information ); } catch { MessageBox.Show("An error occurred while recording the changes! Please, try again!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Paskaitu laiko priskirimo metodas /// </summary> public void Generate() { //Visi galimi laikai List <LectureTime> lecturesTimes = db.LectureTimes.ToList(); //Visos galimso dienos List <Weekday> weekdays = db.Weekdays.ToList(); //Visos klasės su tvarkaraščiais List <ClassRoom> classrooms = db.ClassRooms.ToList(); List <TimeSchedule> classSchedule = new List <TimeSchedule>(); foreach (ClassRoom item in classrooms) { TimeSchedule schedule = new TimeSchedule(item.ID, lecturesTimes.Count); classSchedule.Add(schedule); } //Visi dėstytojai su tvarkaraščiais List <Teacher> teachers = db.Teachers.ToList(); List <TimeSchedule> teachersSchedule = new List <TimeSchedule>(); foreach (Teacher item in teachers) { TimeSchedule schedule = new TimeSchedule(item.ID, lecturesTimes.Count); teachersSchedule.Add(schedule); } //Visos grupės su tvarkaraščiais List <Group> groups = db.Groups.ToList(); List <TimeSchedule> groupsSchedule = new List <TimeSchedule>(); foreach (Group item in groups) { TimeSchedule schedule = new TimeSchedule(item.ID, lecturesTimes.Count); groupsSchedule.Add(schedule); } foreach (Lecture lecture in db.Lectures) { //Išrenkam reikiamus dalykus Teacher currTeacher = lecture.Teacher; Group currGroup = lecture.Group; ClassRoom currClass = lecture.ClassRoom; TimeSchedule currTeacherSched = teachersSchedule.Where(x => x.ID == currTeacher.ID).Select(x => x).FirstOrDefault(); TimeSchedule currGroupChed = groupsSchedule.Where(x => x.ID == currGroup.ID).Select(x => x).FirstOrDefault(); TimeSchedule currClassSched = classSchedule.Where(x => x.ID == currClass.ID).Select(x => x).FirstOrDefault(); //Jei nėra laiko, ieškom laisvo laiko if (lecture.LectureTimeID == null && lecture.WeekdayID == null) { foreach (Weekday day in weekdays) { foreach (LectureTime time in lecturesTimes) { //Patikrinam ar šiuo laiku dėstytojas,grupė ir auditorija yra laisva if (currTeacherSched.isFree(day.ID, time.ID) && currGroupChed.isFree(day.ID, time.ID) && currClassSched.isFree(day.ID, time.ID)) { lecture.LectureTimeID = time.ID; lecture.WeekdayID = day.ID; currTeacherSched.setTrue(day.ID, time.ID); currGroupChed.setTrue(day.ID, time.ID); currClassSched.setTrue(day.ID, time.ID); break; } } //Patirkinam ar radom laiką paskaitai if (lecture.LectureTimeID != null && lecture.WeekdayID != null) { break; } } } //Jei laikas priskirtas pasižymime į tvarkaraščius else { currTeacherSched.setTrue(lecture.WeekdayID ?? 0, lecture.LectureTimeID ?? 0); currGroupChed.setTrue(lecture.WeekdayID ?? 0, lecture.LectureTimeID ?? 0); currClassSched.setTrue(lecture.WeekdayID ?? 0, lecture.LectureTimeID ?? 0); } } db.SaveChanges(); }
/// <summary> /// Apkeicia dvi paskaitas vietomis, jei tai imanoma /// </summary> /// <param name="ID1">Pirmos paskaitos id</param> /// <param name="ID2">Antros paskaitod id</param> /// <param name="msg">Pranesimas konsolei</param> public void Swap2Lectures(int ID1, int ID2, ref string msg) { Lecture first = db.Lectures.Where(x => x.ID == ID1).Select(x => x).FirstOrDefault(); Lecture second = db.Lectures.Find(ID2); if (isPossibleToChange(first, second)) { msg = "Change was successful."; int day = first.WeekdayID ?? 0; int time = first.LectureTimeID ?? 0; first.WeekdayID = second.WeekdayID; first.LectureTimeID = second.LectureTimeID; second.WeekdayID = day; second.LectureTimeID = time; } else { if (isPossibleToChangeGroup(first, second) && isPossibleToChangeGroup(second, first)) { Random rand = new Random(); bool isPossibleToChangeFirstTeacher = isPossibleToChangeTeacher(first, second); bool isPossibleToChangeSecondTeacher = isPossibleToChangeTeacher(second, first); bool isPossibleToChangeFirstClassRoom = isPossibleToChangeClassRoom(first, second); bool isPossibleToChangeSecondClassRoom = isPossibleToChangeClassRoom(second, first); Teacher firstTeacher = null; Teacher secondTeacher = null; ClassRoom firstClassRoom = null; ClassRoom secondClassRoom = null; if (isPossibleToChangeFirstTeacher == false) { Teacher[] teachers = db.Teachers.Where(x => x.Module == first.Teacher.Module && x.ID != first.TeacherID).Select(x => x).ToArray(); if (teachers.Length > 0) { Teacher[] valid = new Teacher[teachers.Length]; int count = 0; foreach (Teacher teacher in teachers) { TimeSchedule teacherSchedule = Schedule(teacher.ID, "TeacherID"); if (teacherSchedule.isFree(second.WeekdayID ?? 0, second.LectureTimeID ?? 0) || second.TeacherID == teacher.ID) { valid[count] = teacher; count++; } } if (count > 0) { firstTeacher = valid.ElementAt(rand.Next(0, count)); msg = "First lecture teacher was changed to " + firstTeacher.LastName + " " + firstTeacher.FirstName + ". "; } } } else { firstTeacher = first.Teacher; } if (isPossibleToChangeSecondTeacher == false) { Teacher[] teachers = db.Teachers.Where(x => x.Module == second.Teacher.Module && x.ID != second.TeacherID).Select(x => x).ToArray(); if (teachers.Length > 0) { Teacher[] valid = new Teacher[teachers.Length]; int count = 0; foreach (Teacher teacher in teachers) { TimeSchedule teacherSchedule = Schedule(teacher.ID, "TeacherID"); if (teacherSchedule.isFree(first.WeekdayID ?? 0, first.LectureTimeID ?? 0) || first.TeacherID == teacher.ID) { valid[count] = teacher; count++; } } if (count > 0) { secondTeacher = valid.ElementAt(rand.Next(0, count)); msg += "Second lecture teacher was changed to " + secondTeacher.LastName + " " + secondTeacher.FirstName + ". "; } } } else { secondTeacher = second.Teacher; } if (isPossibleToChangeFirstClassRoom == false) { ClassRoom[] classRooms; if (first.IsPcRequired == true) { classRooms = db.ClassRooms.Where(x => x.Type == first.ClassRoom.Type && x.ID != first.ClassRoomID && x.Type == first.Type && x.NumberOfPlaces >= first.Group.StudentsCount && x.IsPCavailable == true).Select(x => x).ToArray(); } else { classRooms = db.ClassRooms.Where(x => x.Type == first.ClassRoom.Type && x.ID != first.ClassRoomID && x.Type == first.Type && x.NumberOfPlaces >= first.Group.StudentsCount).Select(x => x).ToArray(); } if (classRooms.Length > 0) { ClassRoom[] valid = new ClassRoom[classRooms.Length]; int count = 0; foreach (ClassRoom classRoomm in classRooms) { TimeSchedule classRoomSchedule = Schedule(classRoomm.ID, "ClassRoomID"); if (classRoomSchedule.isFree(second.WeekdayID ?? 0, second.LectureTimeID ?? 0) || second.ClassRoomID == classRoomm.ID) { valid[count] = classRoomm; count++; } } if (count > 0) { firstClassRoom = valid.ElementAt(rand.Next(0, count)); msg += "First lecture classroom was changed to " + firstClassRoom.Name + ". "; } } } else { firstClassRoom = first.ClassRoom; } if (isPossibleToChangeSecondClassRoom == false) { ClassRoom[] classRooms; if (second.IsPcRequired == true) { classRooms = db.ClassRooms.Where(x => x.Type == second.ClassRoom.Type && x.ID != second.ClassRoomID && x.Type == second.Type && x.NumberOfPlaces >= second.Group.StudentsCount && x.IsPCavailable == true).Select(x => x).ToArray(); } else { classRooms = db.ClassRooms.Where(x => x.Type == second.ClassRoom.Type && x.ID != second.ClassRoomID && x.Type == second.Type && x.NumberOfPlaces >= second.Group.StudentsCount).Select(x => x).ToArray(); } if (classRooms.Length > 0) { ClassRoom[] valid = new ClassRoom[classRooms.Length]; int count = 0; foreach (ClassRoom classRoomm in classRooms) { TimeSchedule classRoomSchedule = Schedule(classRoomm.ID, "ClassRoomID"); if (classRoomSchedule.isFree(first.WeekdayID ?? 0, first.LectureTimeID ?? 0) || first.ClassRoomID == classRoomm.ID) { valid[count] = classRoomm; count++; } } if (count > 0) { secondClassRoom = valid.ElementAt(rand.Next(0, count)); msg += "Second lecture classroom was changed to " + firstClassRoom.Name + ". "; } } } else { secondClassRoom = second.ClassRoom; } if (firstTeacher != null && secondTeacher != null && firstClassRoom != null && secondClassRoom != null) { msg += "Change was successful."; first.Teacher = firstTeacher; first.TeacherID = firstTeacher.ID; first.ClassRoom = firstClassRoom; first.ClassRoomID = firstClassRoom.ID; second.Teacher = secondTeacher; second.TeacherID = secondTeacher.ID; second.ClassRoom = secondClassRoom; second.ClassRoomID = secondClassRoom.ID; int day = first.WeekdayID ?? 0; int time = first.LectureTimeID ?? 0; first.WeekdayID = second.WeekdayID; first.LectureTimeID = second.LectureTimeID; second.WeekdayID = day; second.LectureTimeID = time; } else { msg = "This change is impossible due to teachers/classrooms schedule."; } } else { msg = "This change is impossible due to groups schedule."; } } db.SaveChanges(); }