using Contracts.BindingModels; using Contracts.BusinessLogicContracts; using Contracts.ViewModels; 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 Forms { public partial class CategoryForm : Form { private readonly ICategoryLogic _categoryLogic; public CategoryForm(ICategoryLogic categoryLogic) { InitializeComponent(); _categoryLogic = categoryLogic; LoadCategories(); } private void LoadCategories() { var categories = _categoryLogic.ReadList().ToList(); dataGridViewCategories.DataSource = categories; } private void dataGridViewCategories_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0) { var category = dataGridViewCategories.Rows[e.RowIndex].DataBoundItem as CategoryViewModel; if (category != null) { var model = new CategoryBindingModel { Id = category.Id, Name = category.Name }; _categoryLogic.Update(model); } } } private void dataGridViewCategories_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e) { var category = e.Row.DataBoundItem as CategoryViewModel; if (category != null) { if (MessageBox.Show("Вы уверены, что хотите удалить выбранную категорию?", "Да", MessageBoxButtons.YesNo) == DialogResult.Yes) { _categoryLogic.Delete(new CategoryBindingModel { Id = category.Id }); } else { e.Cancel = true; } } } private void dataGridViewCategories_UserAddedRow(object sender, DataGridViewRowEventArgs e) { var category = new CategoryViewModel { Name = "" }; var model = new CategoryBindingModel { Name = category.Name }; _categoryLogic.Create(model); LoadCategories(); } private void dataGridViewCategories_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Insert) { var category = new CategoryViewModel { Name = "" }; var model = new CategoryBindingModel { Name = category.Name }; _categoryLogic.Create(model); LoadCategories(); } else if (e.KeyCode == Keys.Delete) { if (dataGridViewCategories.SelectedRows.Count > 0) { var selectedCategory = dataGridViewCategories.SelectedRows[0].DataBoundItem as CategoryViewModel; if (selectedCategory != null) { if (MessageBox.Show("Вы уверены, что хотите удалить выбранную категорию?", "Да", MessageBoxButtons.YesNo) == DialogResult.Yes) { _categoryLogic.Delete(new CategoryBindingModel { Id = selectedCategory.Id }); LoadCategories(); } } } } } } }