From 8244f9dd8dfe986558a36bcdd1fe9b471b4fb056 Mon Sep 17 00:00:00 2001 From: malimova Date: Wed, 8 May 2024 14:11:00 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B2=D1=81=D1=91=20=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LabWork/LabWork/FormContact.cs | 3 +- LabWork/LabWork/FormContacts.Designer.cs | 29 ++++---- LabWork/LabWork/FormContacts.cs | 90 +++++++++++++++++++++++- 3 files changed, 106 insertions(+), 16 deletions(-) diff --git a/LabWork/LabWork/FormContact.cs b/LabWork/LabWork/FormContact.cs index 017bcdd..76645df 100644 --- a/LabWork/LabWork/FormContact.cs +++ b/LabWork/LabWork/FormContact.cs @@ -29,8 +29,7 @@ namespace LabWork { var view = _logic.ReadElement(new ContactSearchModel { - Id = - _id.Value + Id = _id.Value }); if (view != null) { diff --git a/LabWork/LabWork/FormContacts.Designer.cs b/LabWork/LabWork/FormContacts.Designer.cs index 9450e7a..6e916cd 100644 --- a/LabWork/LabWork/FormContacts.Designer.cs +++ b/LabWork/LabWork/FormContacts.Designer.cs @@ -28,22 +28,23 @@ /// private void InitializeComponent() { - dataGridView1 = new DataGridView(); + dataGridView = new DataGridView(); buttonAdd = new Button(); buttonUpd = new Button(); buttonRef = new Button(); buttonDel = new Button(); - ((System.ComponentModel.ISupportInitialize)dataGridView1).BeginInit(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); // - // dataGridView1 + // dataGridView // - dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - dataGridView1.Location = new Point(12, 12); - dataGridView1.Name = "dataGridView1"; - dataGridView1.RowHeadersWidth = 62; - dataGridView1.Size = new Size(717, 426); - dataGridView1.TabIndex = 0; + dataGridView.BackgroundColor = Color.LavenderBlush; + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Location = new Point(12, 12); + dataGridView.Name = "dataGridView"; + dataGridView.RowHeadersWidth = 62; + dataGridView.Size = new Size(717, 426); + dataGridView.TabIndex = 0; // // buttonAdd // @@ -63,6 +64,7 @@ buttonUpd.TabIndex = 2; buttonUpd.Text = "Изменить"; buttonUpd.UseVisualStyleBackColor = true; + buttonUpd.Click += buttonUpd_Click; // // buttonRef // @@ -72,6 +74,7 @@ buttonRef.TabIndex = 3; buttonRef.Text = "Обновить"; buttonRef.UseVisualStyleBackColor = true; + buttonRef.Click += buttonRef_Click; // // buttonDel // @@ -81,6 +84,7 @@ buttonDel.TabIndex = 4; buttonDel.Text = "Удалить"; buttonDel.UseVisualStyleBackColor = true; + buttonDel.Click += buttonDel_Click; // // FormContacts // @@ -91,16 +95,17 @@ Controls.Add(buttonRef); Controls.Add(buttonUpd); Controls.Add(buttonAdd); - Controls.Add(dataGridView1); + Controls.Add(dataGridView); Name = "FormContacts"; Text = "Менеджер контактов"; - ((System.ComponentModel.ISupportInitialize)dataGridView1).EndInit(); + Load += FormContacts_Load; + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); ResumeLayout(false); } #endregion - private DataGridView dataGridView1; + private DataGridView dataGridView; private Button buttonAdd; private Button buttonUpd; private Button buttonRef; diff --git a/LabWork/LabWork/FormContacts.cs b/LabWork/LabWork/FormContacts.cs index a148b77..24cc47b 100644 --- a/LabWork/LabWork/FormContacts.cs +++ b/LabWork/LabWork/FormContacts.cs @@ -7,19 +7,105 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using LabWork.Interfaces; namespace LabWork { public partial class FormContacts : Form { - public FormContacts() + private readonly IContactLogic _logic; + public FormContacts(IContactLogic logic) { InitializeComponent(); + _logic = logic; + } + private void FormContacts_Load(object sender, EventArgs e) + { + LoadData(); + } + private void LoadData() + { + try + { + var list = _logic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["ContactName"].AutoSizeMode = + DataGridViewAutoSizeColumnMode.Fill; + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } } private void buttonAdd_Click(object sender, EventArgs e) { - + var service = Program.ServiceProvider?.GetService(typeof(FormContact)); + if (service is FormContact form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } } + + private void buttonUpd_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = + Program.ServiceProvider?.GetService(typeof(FormContact)); + if (service is FormContact form) + { + form.Id = + Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + + private void buttonRef_Click(object sender, EventArgs e) + { + LoadData(); + } + + private void buttonDel_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 (!_logic.Delete(new ContactBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + } }