//Кнопка "Изменить" private void buttonEdit_Click(object sender, EventArgs e) { try { if (listViewStudents.SelectedItems.Count == 1) { if (comboBoxGroups.SelectedItem != null && textBoxFirstName.Text != "" && textBoxMiddleName.Text != "" && textBoxLastName.Text != "" && dateTimePickerYear.Value != null) { StudentsSet studentsSet = listViewStudents.SelectedItems[0].Tag as StudentsSet; studentsSet.IdGroup = Convert.ToInt32(comboBoxGroups.SelectedItem.ToString().Split('.')[0]); studentsSet.FirstName = textBoxFirstName.Text; studentsSet.MiddleName = textBoxMiddleName.Text; studentsSet.LastName = textBoxLastName.Text; studentsSet.YearPost = dateTimePickerYear.Value; Program.ekzDb.SaveChanges(); ShowStudents(); } } else { MessageBox.Show("Поля не заполнены! Проверьте и повторите попытку.", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } catch (Exception ex) { MessageBox.Show("" + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
//Кнопка "Удалить" private void buttonDel_Click(object sender, EventArgs e) { try { if (listViewStudents.SelectedItems.Count == 1) { StudentsSet studentsSet = listViewStudents.SelectedItems[0].Tag as StudentsSet; Program.ekzDb.StudentsSet.Remove(studentsSet); Program.ekzDb.SaveChanges(); ShowStudents(); } comboBoxGroups.SelectedItem = null; dateTimePickerYear.Value = DateTime.Now; textBoxFirstName.Text = ""; textBoxMiddleName.Text = ""; textBoxLastName.Text = ""; } catch { MessageBox.Show("Невозможно удалить, эта запись используется!", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
//Вывод информации private void listViewStudents_SelectedIndexChanged(object sender, EventArgs e) { if (listViewStudents.SelectedItems.Count == 1) { StudentsSet studentsSet = listViewStudents.SelectedItems[0].Tag as StudentsSet; comboBoxGroups.SelectedIndex = comboBoxGroups.FindString(studentsSet.IdGroup.ToString()); dateTimePickerYear.Value = studentsSet.YearPost; textBoxFirstName.Text = studentsSet.FirstName; textBoxMiddleName.Text = studentsSet.MiddleName; textBoxLastName.Text = studentsSet.LastName; } else { comboBoxGroups.SelectedItem = null; dateTimePickerYear.Value = DateTime.Now; textBoxFirstName.Text = ""; textBoxMiddleName.Text = ""; textBoxLastName.Text = ""; } }