/// <summary> /// Initializes a new instance of the form EditSubject class. /// </summary> /// <param name="model">Subject details to edit.</param> public EditSubject(Subject model) { InitializeComponent(); this.model = model; subjectController = new SubjectController(); gradeController = new GradeController(); grades = gradeController.Details(); subjects = subjectController.Details(); txtName.Text = model.Name; }
/// <summary> /// Determine which type to load into the DataGridView. /// </summary> /// <param name="obj">Typeof the object.</param> public void LoadToGrid(Type obj) { byte rowIndex = 0; byte columnIndex = 0; switch (obj.ToString()) { case "SamenSterkOnline.Models.Task": dgvShedule.Rows.Clear(); tasks = taskController.Details(selectedUser.Id); repeatingTasks = repeatingTaskController.Details(selectedUser.Id); for (int i = 0; i < 17; i++) { dgvShedule.Rows.Add(); dgvShedule.Rows[i].HeaderCell.Value = (7 + i) + ":00"; } List <Task> taskQuery = (from task in tasks where task.Date >= Convert.ToDateTime(dgvShedule.Columns[0].HeaderText) && task.Date <= Convert.ToDateTime(dgvShedule.Columns[6].HeaderText) select task).ToList(); if (taskQuery.Count != 0) { foreach (Task element in taskQuery) { for (byte i = 0; i < 17; i++) { if (dgvShedule.Rows[i].HeaderCell.Value.ToString() == element.Date.ToString("H:mm")) { rowIndex = i; } } for (byte i = 0; i < 7; i++) { if (dgvShedule.Columns[i].HeaderText == element.Date.ToString("dd-MM-yyyy")) { columnIndex = i; } } dgvShedule.Rows[rowIndex].Cells[columnIndex].Value = element.Title; for (byte i = 0; i <= element.Duration - 1; i++) { if (rowIndex + i < dgvShedule.RowCount) { dgvShedule.Rows[rowIndex + i].Cells[columnIndex].Style.BackColor = ColorTranslator.FromHtml("#673AB7"); dgvShedule.Rows[rowIndex + i].Cells[columnIndex].Style.ForeColor = Color.White; } } rowIndex = 0; columnIndex = 0; } } if (repeatingTasks.Count != 0) { foreach (RepeatingTask element in repeatingTasks) { for (byte i = 0; i < 17; i++) { //Checks if the header time is longer than 4 characters if (dgvShedule.Rows[i].HeaderCell.Value.ToString().Length == 4) { if (Convert.ToInt32(dgvShedule.Rows[i].HeaderCell.Value.ToString().Substring(0, 1)) == element.Time.Hours) { rowIndex = i; } } else { if (Convert.ToInt32(dgvShedule.Rows[i].HeaderCell.Value.ToString().Substring(0, 2)) == element.Time.Hours) { rowIndex = i; } } } for (byte i = 0; i < 7; i++) { if (Convert.ToDateTime(dgvShedule.Columns[i].HeaderText).ToString("dddd") == element.Day) { columnIndex = i; } } dgvShedule.Rows[rowIndex].Cells[columnIndex].Value = element.Title; for (byte i = 0; i <= element.Duration - 1; i++) { dgvShedule.Rows[rowIndex + i].Cells[columnIndex].Style.BackColor = ColorTranslator.FromHtml("#7986CB"); dgvShedule.Rows[rowIndex + i].Cells[columnIndex].Style.ForeColor = Color.White; } rowIndex = 0; columnIndex = 0; } } break; case "SamenSterkOnline.Models.Grade": int index = -1; dgvGrades.CellValueChanged -= dgvGrades_CellValueChanged; dgvGrades.Rows.Clear(); dgvGrades.ColumnCount = 1; grades = gradeController.Details(selectedUser.Id); subjects = subjectController.Details(selectedUser.Id); if (grades.Count != 0) { dgvGrades.ColumnCount = grades.Max(grade => Convert.ToInt32(grade.ColumnIndex)) + 1; } if (subjects.Count != 0) { for (int i = 0; i < subjects.Count; i++) { dgvGrades.Rows.Add(); dgvGrades.Rows[i].HeaderCell.Value = subjects[i].Name; } if (grades.Count != 0) { for (int i = 0; i < grades.Count; i++) { if (grades[i].Number == 10.0f) { dgvGrades[Convert.ToInt32(grades[i].ColumnIndex), Convert.ToInt32(grades[i].RowIndex)].Value = grades[i].Number.ToString("0").Replace('.', ','); } else { dgvGrades[Convert.ToInt32(grades[i].ColumnIndex), Convert.ToInt32(grades[i].RowIndex)].Value = grades[i].Number.ToString("0.0").Replace('.', ','); } } if (dgvGrades.ColumnCount > 0) { List <DataGridViewRow> rows = (from row in dgvGrades.Rows.Cast <DataGridViewRow>() select row).ToList(); if (rows.Count != 0) { index = (from row in rows where row.Cells[dgvGrades.ColumnCount - 1].Value != null select row.Index).FirstOrDefault(); } } } } if (index != -1) { dgvGrades.ColumnCount += 1; } dgvGrades.CellValueChanged += dgvGrades_CellValueChanged; break; case "SamenSterkOnline.Models.Appointment": DataTable dataTable = new DataTable(); dgvAppointments.CellValueChanged -= dgvAppointments_CellValueChanged; dgvAppointments.DataSource = null; dgvAppointments.Rows.Clear(); appointments = appointmentController.Details(selectedUser.Id); appointments = (from appointment in appointments where appointment.Date > DateTime.Today || appointment.Date == new DateTime(1980, 1, 1) select appointment).ToList(); dataTable.Columns.Add("Naam", typeof(string)); dataTable.Columns.Add("Datum", typeof(string)); dataTable.Columns.Add("Verwijderen?", typeof(bool)); dataTable.Columns.Add("Id", typeof(uint)); if (appointments.Count != 0) { for (int i = 0; i < appointments.Count; i++) { if (appointments[i].Date.ToString("dd-MM-yyyy HH:mm") == new DateTime(1980, 1, 1).ToString("dd-MM-yyyy HH:mm")) { dataTable.Rows.Add(appointments[i].Name, "[Geen datum]", false, appointments[i].Id); } else { dataTable.Rows.Add(appointments[i].Name, appointments[i].Date.ToString("dd-MM-yyyy HH:mm"), false, appointments[i].Id); } } } dgvAppointments.DataSource = dataTable; dgvAppointments.Columns[1].ReadOnly = true; dgvAppointments.Columns[1].Resizable = DataGridViewTriState.False; dgvAppointments.Columns[2].Resizable = DataGridViewTriState.False; dgvAppointments.Columns[dgvAppointments.ColumnCount - 1].Visible = false; dgvAppointments.CellValueChanged += dgvAppointments_CellValueChanged; break; default: break; } }
/// <summary> /// Occurs when the value of a cell changes. Adds a new Appointment if there wasn't no value. Edit an existing Appointment if the value changed. Deletes an existing Appointment if the value is empty. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="DataGridViewCellEventArgs"/> instance containing the event data.</param> private void dgvUsers_CellValueChanged(object sender, DataGridViewCellEventArgs e) { dgvUsers.CellValueChanged -= dgvUsers_CellValueChanged; dgvUsers[e.ColumnIndex, e.RowIndex].Value = false; dgvUsers.CellValueChanged += dgvUsers_CellValueChanged; if (e.ColumnIndex == 1) { DialogResult dialogResult = MessageBox.Show("U staat op het punt om deze gebruiker te verwijderen.\nAlle taken, vakken, cijfers en afspraken zullen van deze gebruiker verwijderd worden.", "Gebruiker verwijderen", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { int result = 0; User query = null; uint?id = null; List <DataGridViewRow> rows = (from row in dgvUsers.Rows.Cast <DataGridViewRow>() select row).ToList(); id = (from row in rows where Convert.ToBoolean(row.Cells["verwijderen?"].Value) == true select Convert.ToUInt32(row.Cells[dgvUsers.ColumnCount - 1].Value)).FirstOrDefault(); query = (from user in users where user.Id == id select user).FirstOrDefault(); if (query != null) { tasks = taskController.Details(id); repeatingTasks = repeatingTaskController.Details(id); grades = gradeController.Details(id); subjects = subjectController.Details(id); appointments = appointmentController.Details(id); for (int i = 0; i < tasks.Count; i++) { taskController.Delete(tasks[i]); } for (int i = 0; i < repeatingTasks.Count; i++) { repeatingTaskController.Delete(repeatingTasks[i]); } for (int i = 0; i < grades.Count; i++) { gradeController.Delete(grades[i]); } for (int i = 0; i < subjects.Count; i++) { subjectController.Delete(subjects[i]); } for (int i = 0; i < appointments.Count; i++) { appointmentController.Delete(appointments[i]); } result = userController.Delete(query); if (result == 0) { MessageBox.Show("Kon de gebruiker niet verwijderen."); } else { MessageBox.Show("De gebruiker is succesvol verwijderd."); } } LoadToGrid(); } } }