From 6aad82322f1384fe44c7ce91683f0b726c3766de Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 22 Apr 2023 21:24:46 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B4=D1=83=D1=85=D0=BE=D1=82=D0=B0=20=D1=81?= =?UTF-8?q?=20=D1=82=D0=B0=D0=B1=D0=BB=D0=B8=D1=87=D0=BA=D0=B0=D0=BC=D0=B8?= =?UTF-8?q?....?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LawFirm/LawFirm/DataGridViewExtension.cs | 50 ++++++++++++++++++++++++ LawFirm/LawFirm/FormBlanks.cs | 9 +---- LawFirm/LawFirm/FormClients.cs | 8 +--- LawFirm/LawFirm/FormDocuments.cs | 9 +---- LawFirm/LawFirm/FormImplementers.cs | 8 +--- LawFirm/LawFirm/FormMails.cs | 11 +----- LawFirm/LawFirm/FormMain.cs | 9 +---- 7 files changed, 58 insertions(+), 46 deletions(-) create mode 100644 LawFirm/LawFirm/DataGridViewExtension.cs diff --git a/LawFirm/LawFirm/DataGridViewExtension.cs b/LawFirm/LawFirm/DataGridViewExtension.cs new file mode 100644 index 0000000..9770c3e --- /dev/null +++ b/LawFirm/LawFirm/DataGridViewExtension.cs @@ -0,0 +1,50 @@ +using LawFirmContracts.Attributes; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmView +{ + 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/LawFirm/LawFirm/FormBlanks.cs b/LawFirm/LawFirm/FormBlanks.cs index bc6a9ff..1656231 100644 --- a/LawFirm/LawFirm/FormBlanks.cs +++ b/LawFirm/LawFirm/FormBlanks.cs @@ -1,4 +1,5 @@ using LawFirm; +using LawFirmBusinessLogic.BusinessLogics; using LawFirmContracts.BindingModels; using LawFirmContracts.BusinessLogicContracts; using Microsoft.Extensions.Logging; @@ -34,13 +35,7 @@ namespace LawFirmView { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["BlankName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } + dataGridView.FillAndConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Загрузка бланков"); } catch (Exception ex) diff --git a/LawFirm/LawFirm/FormClients.cs b/LawFirm/LawFirm/FormClients.cs index 1f7c892..3e7bb7d 100644 --- a/LawFirm/LawFirm/FormClients.cs +++ b/LawFirm/LawFirm/FormClients.cs @@ -33,13 +33,7 @@ namespace LawFirmView { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["ClientFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } + dataGridView.FillAndConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Загрузка клиентов"); } catch (Exception ex) diff --git a/LawFirm/LawFirm/FormDocuments.cs b/LawFirm/LawFirm/FormDocuments.cs index 6bedfc7..1b4f6c7 100644 --- a/LawFirm/LawFirm/FormDocuments.cs +++ b/LawFirm/LawFirm/FormDocuments.cs @@ -33,14 +33,7 @@ namespace LawFirmView { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["DocumentName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - dataGridView.Columns["DocumentBlanks"].Visible = false; - } + dataGridView.FillAndConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Загрузка документов"); } catch (Exception ex) diff --git a/LawFirm/LawFirm/FormImplementers.cs b/LawFirm/LawFirm/FormImplementers.cs index c37b690..0d4a8f0 100644 --- a/LawFirm/LawFirm/FormImplementers.cs +++ b/LawFirm/LawFirm/FormImplementers.cs @@ -33,13 +33,7 @@ namespace LawFirmView { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["ImplementerFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } + dataGridView.FillAndConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Загрузка исполнителей"); } catch (Exception ex) diff --git a/LawFirm/LawFirm/FormMails.cs b/LawFirm/LawFirm/FormMails.cs index b3e9263..4b0c499 100644 --- a/LawFirm/LawFirm/FormMails.cs +++ b/LawFirm/LawFirm/FormMails.cs @@ -28,15 +28,8 @@ namespace LawFirmView { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["ClientId"].Visible = false; - dataGridView.Columns["MessageId"].Visible = false; - dataGridView.Columns["Body"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } - _logger.LogInformation("Загрузка писем"); + dataGridView.FillAndConfigGrid(_logic.ReadList(null)); + _logger.LogInformation("Загрузка писем"); } catch (Exception ex) { diff --git a/LawFirm/LawFirm/FormMain.cs b/LawFirm/LawFirm/FormMain.cs index 1e1ad9a..536d155 100644 --- a/LawFirm/LawFirm/FormMain.cs +++ b/LawFirm/LawFirm/FormMain.cs @@ -41,14 +41,7 @@ namespace LawFirmView _logger.LogInformation("Загрузка заказов"); try { - var list = _orderLogic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["DocumentId"].Visible = false; - dataGridView.Columns["ClientId"].Visible = false; - dataGridView.Columns["ImplementerId"].Visible = false; - } + dataGridView.FillAndConfigGrid(_orderLogic.ReadList(null)); _logger.LogInformation("Загрузка заказов"); } catch (Exception ex)