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 FormRoles : Form { private readonly IRoleLogic _roleLogic; public FormRoles(IRoleLogic roleLogic) { InitializeComponent(); _roleLogic = roleLogic; } private void buttonCreate_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormRole)); if (service is FormRole form) { if (form.ShowDialog() == DialogResult.OK) { LoadData(); } } } private void LoadData() { try { var list = _roleLogic.ReadList(null); if (list != null) { dataGridView.DataSource = list; dataGridView.Columns["Id"].Visible = false; dataGridView.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } } catch (Exception) { } } private void FormRoles_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 (!_roleLogic.Delete(new RoleBindingModel { 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(FormRole)); if (service is FormRole 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(); } } }