From f587a9cad625e0688064a276c72cf8e4009dc1f7 Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Sun, 19 May 2024 19:33:18 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B0=D0=BD=D0=B0=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4=D0=B0=20=D0=B8=D0=BD=D1=84=D0=BE?= =?UTF-8?q?=D1=80=D0=BC=D0=B0=D1=86=D0=B8=D0=B8=20=D0=B2=20=D1=82=D0=B0?= =?UTF-8?q?=D0=B1=D0=BB=D0=B8=D1=86=D1=8B=20DataGridView?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SecuritySystemView/Client/FormClients.cs | 10 +---- .../Component/FormComponents.cs | 7 +-- .../DataGridViewExtension.cs | 45 +++++++++++++++++++ SecuritySystem/SecuritySystemView/FormMain.cs | 23 +--------- .../Implementer/FormImplementers.cs | 10 +---- .../SecuritySystemView/Mail/FormMails.cs | 9 +--- .../SecuritySystemView/Secure/FormSecures.cs | 9 +--- 7 files changed, 53 insertions(+), 60 deletions(-) create mode 100644 SecuritySystem/SecuritySystemView/DataGridViewExtension.cs diff --git a/SecuritySystem/SecuritySystemView/Client/FormClients.cs b/SecuritySystem/SecuritySystemView/Client/FormClients.cs index 2091f56..08dc491 100644 --- a/SecuritySystem/SecuritySystemView/Client/FormClients.cs +++ b/SecuritySystem/SecuritySystemView/Client/FormClients.cs @@ -23,15 +23,7 @@ namespace SecuritySystemView.Client { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader; - dataGridView.Columns["ClientFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - dataGridView.Columns["Email"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - dataGridView.Columns["Email"].FillWeight = 50; - } + dataGridView.FillandConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Загрузка клиентов"); } catch (Exception ex) diff --git a/SecuritySystem/SecuritySystemView/Component/FormComponents.cs b/SecuritySystem/SecuritySystemView/Component/FormComponents.cs index 43c569e..9acae52 100644 --- a/SecuritySystem/SecuritySystemView/Component/FormComponents.cs +++ b/SecuritySystem/SecuritySystemView/Component/FormComponents.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.Logging; using SecuritySystemContracts.BindingModels; using SecuritySystemContracts.BusinessLogicsContracts; +using System.Windows.Forms; namespace SecuritySystemView { @@ -22,11 +23,7 @@ namespace SecuritySystemView { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridViewComponents.DataSource = list; - } + dataGridViewComponents.FillandConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Загрузка компонентов"); } catch (Exception ex) diff --git a/SecuritySystem/SecuritySystemView/DataGridViewExtension.cs b/SecuritySystem/SecuritySystemView/DataGridViewExtension.cs new file mode 100644 index 0000000..0bd3f43 --- /dev/null +++ b/SecuritySystem/SecuritySystemView/DataGridViewExtension.cs @@ -0,0 +1,45 @@ +using SecuritySystemContracts.Attributes; + +namespace SecuritySystemView +{ + public static class DataGridViewExtension + { + public static void FillandConfigGrid(this DataGridView grid, List? data) + { + if (data == null) + { + return; + } + grid.DataSource = data; + var type = typeof(T); + var properties = type.GetProperties(); + foreach (DataGridViewColumn column in grid.Columns) + { + var property = properties.FirstOrDefault(x => x.Name == column.Name); + if (property == null) + { + throw new InvalidOperationException($"В типе {type.Name} не найдено свойство с именем {column.Name}"); + } + var attribute = property.GetCustomAttributes(typeof(ColumnAttribute), true)?.SingleOrDefault(); + if (attribute == null) + { + throw new InvalidOperationException($"Не найден атрибут типа ColumnAttribute для свойства {property.Name}"); + } + if (attribute is ColumnAttribute columnAttr) + { + column.HeaderText = columnAttr.Title; + column.Visible = columnAttr.Visible; + if (columnAttr.IsUseAutoSize) + { + column.AutoSizeMode = (DataGridViewAutoSizeColumnMode)Enum.Parse(typeof(DataGridViewAutoSizeColumnMode), columnAttr.GridViewAutoSize.ToString()); + } + else + { + column.Width = columnAttr.Width; + } + } + } + } + } + +} diff --git a/SecuritySystem/SecuritySystemView/FormMain.cs b/SecuritySystem/SecuritySystemView/FormMain.cs index 4e7bc1a..c0b4fbf 100644 --- a/SecuritySystem/SecuritySystemView/FormMain.cs +++ b/SecuritySystem/SecuritySystemView/FormMain.cs @@ -28,30 +28,9 @@ namespace SecuritySystemView } private void LoadData() { - _logger.LogInformation("Загрузка заказов"); try { - var list = _orderLogic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["SecureId"].Visible = false; - dataGridView.Columns["ClientId"].Visible = false; - dataGridView.Columns["ImplementerId"].Visible = false; - dataGridView.Columns["Count"].AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader; - dataGridView.Columns["Count"].HeaderText = "Кол-во"; - dataGridView.Columns["Count"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; - dataGridView.Columns["Sum"].AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader; - dataGridView.Columns["Sum"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; - dataGridView.Columns["Id"].AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader; - dataGridView.Columns["Status"].AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader; - dataGridView.Columns["Status"].HeaderText = "Статус заказа"; - dataGridView.Columns["DateCreate"].AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader; - dataGridView.Columns["DateImplement"].AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader; - dataGridView.Columns["ClientFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - dataGridView.Columns["ImplementerFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - dataGridView.Columns["SecureName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } + dataGridView.FillandConfigGrid(_orderLogic.ReadList(null)); _logger.LogInformation("Загрузка заказов"); } catch (Exception ex) diff --git a/SecuritySystem/SecuritySystemView/Implementer/FormImplementers.cs b/SecuritySystem/SecuritySystemView/Implementer/FormImplementers.cs index 4f9ac3d..aef6cfb 100644 --- a/SecuritySystem/SecuritySystemView/Implementer/FormImplementers.cs +++ b/SecuritySystem/SecuritySystemView/Implementer/FormImplementers.cs @@ -22,15 +22,7 @@ namespace SecuritySystemView.Implementer { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["WorkExperience"].AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader; - dataGridView.Columns["ImplementerFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - dataGridView.Columns["Qualification"].AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader; - } + dataGridView.FillandConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Загрузка исполнителей"); } catch (Exception ex) diff --git a/SecuritySystem/SecuritySystemView/Mail/FormMails.cs b/SecuritySystem/SecuritySystemView/Mail/FormMails.cs index f31cab7..d04798e 100644 --- a/SecuritySystem/SecuritySystemView/Mail/FormMails.cs +++ b/SecuritySystem/SecuritySystemView/Mail/FormMails.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Logging; +using SecuritySystemBusinessLogic.BusinessLogics; using SecuritySystemContracts.BusinessLogicsContracts; namespace SecuritySystemView.Mail @@ -22,13 +23,7 @@ namespace SecuritySystemView.Mail { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["MessageId"].Visible = false; - dataGridView.Columns["ClientId"].Visible = false; - } + dataGridView.FillandConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Загрузка почты"); } catch (Exception ex) diff --git a/SecuritySystem/SecuritySystemView/Secure/FormSecures.cs b/SecuritySystem/SecuritySystemView/Secure/FormSecures.cs index 36b76ba..1516778 100644 --- a/SecuritySystem/SecuritySystemView/Secure/FormSecures.cs +++ b/SecuritySystem/SecuritySystemView/Secure/FormSecures.cs @@ -22,14 +22,7 @@ namespace SecuritySystemView { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["SecureName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - dataGridView.Columns["SecureComponents"].Visible = false; - } + dataGridView.FillandConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Загрузка изделий"); } catch (Exception ex)