using Contracts.BindingModels; using Contracts.SearchModels; using Contracts.StoragesContracts; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WinForm { public partial class FormShapes : Form { IShapeStorage _shapeStorage; public FormShapes(IShapeStorage shapeStorage) { _shapeStorage = shapeStorage; InitializeComponent(); LoadData(); } private void LoadData() { try { var list = _shapeStorage.GetFullList(); if (list != null) { dataGridView1.DataSource = list; dataGridView1.Columns["Id"].Visible = false; dataGridView1.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void AddShape() { var list = _shapeStorage.GetFullList(); list.Add(new()); if (list != null) { dataGridView1.DataSource = list; dataGridView1.Columns["Id"].Visible = false; dataGridView1.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } } private void dataGridView1_KeyDown(object sender, KeyEventArgs e) { //просто найти кнопку if (e.Control) { if (e.KeyCode == Keys.Delete) { AddShape(); } } else if (e.KeyCode == Keys.Delete) { RemoveShape(); } } private void RemoveShape() { if (dataGridView1.SelectedRows.Count > 0) { DialogResult result = MessageBox.Show( "Вы уверены, что хотите удалить выбранные записи?", "Подтверждение удаления", MessageBoxButtons.YesNo, MessageBoxIcon.Question ); if (result == DialogResult.Yes) { if (MessageBox.Show("Удалить выбранный элемент", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { _shapeStorage.Delete(new ShapeBindingModel() { Id = (int)dataGridView1.CurrentRow.Cells[1].Value }); LoadData(); } } LoadData(); } else { MessageBox.Show("Выберите"); } } private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0) { DataGridViewRow row = dataGridView1.Rows[e.RowIndex]; int id = Convert.ToInt32(row.Cells["Id"].Value); string? name = row.Cells["Name"].Value?.ToString(); if (string.IsNullOrWhiteSpace(name)) { MessageBox.Show("Нельзя сохранить запись с пустым именем!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); LoadData(); } else { var model = new ShapeBindingModel { Id = id, Name = name }; if (model.Id == 0) { _shapeStorage.Insert(model); } else { _shapeStorage.Update(model); } LoadData(); } } } } }