using ElectronicDiaryAbstractions.Models; using ElectronicDiaryAbstractions.WorkAbstractions; namespace ElectronicDiaryView { public partial class FormGrade : Form { private readonly IGradeWork _gradeLogic; private readonly ITeacherWork _teacherLogic; public FormGrade(IGradeWork gradeLogic, ITeacherWork teacherLogic) { InitializeComponent(); _gradeLogic = gradeLogic; _teacherLogic = teacherLogic; } private void FormGrade_Load(object sender, EventArgs e) { LoadData(); } private void LoadData() { var grades = _gradeLogic.GetAll(); dataGridView.Rows.Clear(); if (dataGridView.ColumnCount == 0) { dataGridView.Columns.Add("Id", "ID"); dataGridView.Columns.Add("Name", "Наименование класса"); dataGridView.Columns.Add("TeacherId", "TeacherId"); dataGridView.Columns["TeacherId"].Visible = false; dataGridView.Columns.Add("TeacherName", "Классный руководитель"); } dataGridView.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView.Columns["TeacherName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; comboBoxTeacher.DataSource = _teacherLogic.GetAll().Select(x => new { x.Id, FullName = x.LastName + " " + x.FirstName + " " + x.Patronymic } ).ToList(); comboBoxTeacher.DisplayMember = "FullName"; comboBoxTeacher.ValueMember = "Id"; foreach (var grade in grades) { var teacher = _teacherLogic.Get(grade.TeacherId); dataGridView.Rows.Add(grade.Id, grade.Name, grade.TeacherId, teacher?.LastName + " " + teacher?.FirstName + " " + teacher?.Patronymic); } } private void ButtonCreate_Click(object sender, EventArgs e) { Grade newGrade = new() { Name = textBoxName.Text, TeacherId = ((dynamic)comboBoxTeacher.SelectedItem).Id, }; _gradeLogic.Create(newGrade); LoadData(); } private void ButtonUpdate_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count > 0) { DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; int gradeId = Convert.ToInt32(selectedRow.Cells["Id"].Value); Grade updatedGrade = new() { Id = gradeId, Name = textBoxName.Text, TeacherId = ((dynamic)comboBoxTeacher.SelectedItem).Id, }; _gradeLogic.Update(updatedGrade); LoadData(); } else { MessageBox.Show("Пожалуйста, выберите класс, который необходимо обновить"); } } private void ButtonDelete_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count > 0) { DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; int gradeId = Convert.ToInt32(selectedRow.Cells["Id"].Value); _gradeLogic.Delete(gradeId); LoadData(); } else { MessageBox.Show("Пожалуйста, выберите класс, который необходимо удалить"); } } private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0) { DataGridViewRow row = dataGridView.Rows[e.RowIndex]; textBoxName.Text = row.Cells["Name"].Value.ToString(); comboBoxTeacher.SelectedValue = Convert.ToInt32(row.Cells["TeacherId"].Value); } } } }