using ForumContracts.BindingModels; using ForumContracts.BusinessLogicContracts; 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 Forum { public partial class FormCategories : Form { private readonly ICategoryLogic _categoryLogic; public FormCategories(ICategoryLogic categoryLogic) { InitializeComponent(); _categoryLogic = categoryLogic; } private void buttonCreate_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormCategory)); if (service is FormCategory form) { if (form.ShowDialog() == DialogResult.OK) { LoadData(); } } } private void LoadData() { try { var list = _categoryLogic.ReadList(null); if (list != null) { dataGridView.DataSource = list; dataGridView.Columns["Id"].Visible = false; dataGridView.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } } catch (Exception) { } } private void FormCategories_Load(object sender, EventArgs e) { LoadData(); } private void buttonDelete_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count == 1) { if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); try { if (!_categoryLogic.Delete(new CategoryBindingModel { Id = id })) { throw new Exception("Ошибка при удалении."); } LoadData(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } private void buttonChange_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count == 1) { var service = Program.ServiceProvider?.GetService(typeof(FormCategory)); if (service is FormCategory form) { form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); if (form.ShowDialog() == DialogResult.OK) { LoadData(); } } } } private void buttonUpdate_Click(object sender, EventArgs e) { LoadData(); } private void buttonAddTopicInCategory_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count == 1) { var service = Program.ServiceProvider?.GetService(typeof(FormAddTopics)); if (service is FormAddTopics form) { form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); if (form.ShowDialog() == DialogResult.OK) { LoadData(); } } } } } }