diff --git a/FlowerShop/FlowerShop/DataGridViewExtension.cs b/FlowerShop/FlowerShop/DataGridViewExtension.cs new file mode 100644 index 0000000..4526cb0 --- /dev/null +++ b/FlowerShop/FlowerShop/DataGridViewExtension.cs @@ -0,0 +1,47 @@ +using FlowerShopContracts.Attributes; + +namespace FlowerShopView +{ + 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/FlowerShop/FlowerShop/FormClients.cs b/FlowerShop/FlowerShop/FormClients.cs index 036ee83..561c05a 100644 --- a/FlowerShop/FlowerShop/FormClients.cs +++ b/FlowerShop/FlowerShop/FormClients.cs @@ -24,12 +24,7 @@ namespace FlowerShopView { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - } + dataGridView.FillandConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Загрузка клиентов"); } catch (Exception ex) diff --git a/FlowerShop/FlowerShop/FormComponents.cs b/FlowerShop/FlowerShop/FormComponents.cs index dd94bec..54b2d27 100644 --- a/FlowerShop/FlowerShop/FormComponents.cs +++ b/FlowerShop/FlowerShop/FormComponents.cs @@ -22,14 +22,7 @@ namespace FlowerShopView { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["ComponentName"].AutoSizeMode = - DataGridViewAutoSizeColumnMode.Fill; - } + dataGridView.FillandConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Загрузка компонентов"); } catch (Exception ex) diff --git a/FlowerShop/FlowerShop/FormFlowers.cs b/FlowerShop/FlowerShop/FormFlowers.cs index fa703b7..7827afe 100644 --- a/FlowerShop/FlowerShop/FormFlowers.cs +++ b/FlowerShop/FlowerShop/FormFlowers.cs @@ -24,19 +24,12 @@ namespace FlowerShopView { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["FlowerComponents"].Visible = false; - dataGridView.Columns["FlowerName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } - _logger.LogInformation("Загрузка компонентов"); + dataGridView.FillandConfigGrid(_logic.ReadList(null)); + _logger.LogInformation("Загрузка цветов"); } catch (Exception ex) { - _logger.LogError(ex, "Ошибка загрузки компонентов"); + _logger.LogError(ex, "Ошибка загрузки цветов"); } } diff --git a/FlowerShop/FlowerShop/FormImplementers.cs b/FlowerShop/FlowerShop/FormImplementers.cs index 4ebd464..1efaf35 100644 --- a/FlowerShop/FlowerShop/FormImplementers.cs +++ b/FlowerShop/FlowerShop/FormImplementers.cs @@ -21,15 +21,7 @@ namespace FlowerShopView { try { - var List = _implementerLogic.ReadList(null); - - if (List != null) - { - DataGridView.DataSource = List; - DataGridView.Columns["Id"].Visible = false; - DataGridView.Columns["ImplementerFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } - + DataGridView.FillandConfigGrid(_implementerLogic.ReadList(null)); _logger.LogInformation("Загрузка исполнителей"); } catch (Exception ex) diff --git a/FlowerShop/FlowerShop/FormMail.cs b/FlowerShop/FlowerShop/FormMail.cs index 72f0ca0..fae2a63 100644 --- a/FlowerShop/FlowerShop/FormMail.cs +++ b/FlowerShop/FlowerShop/FormMail.cs @@ -20,16 +20,7 @@ namespace FlowerShopView { try { - var List = _messageLogic.ReadList(null); - - if (List != null) - { - dataGridView.DataSource = List; - dataGridView.Columns["MessageId"].Visible = false; - dataGridView.Columns["ClientId"].Visible = false; - dataGridView.Columns["Body"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } - + dataGridView.FillandConfigGrid(_messageLogic.ReadList(null)); _logger.LogInformation("Загрузка почтовых собщений"); } catch (Exception ex) diff --git a/FlowerShop/FlowerShop/FormMain.cs b/FlowerShop/FlowerShop/FormMain.cs index 44ea087..7c50009 100644 --- a/FlowerShop/FlowerShop/FormMain.cs +++ b/FlowerShop/FlowerShop/FormMain.cs @@ -25,18 +25,10 @@ namespace FlowerShopView } private void LoadData() { - _logger.LogInformation("Загрузка заказов"); try { - var list = _orderLogic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["FlowerId"].Visible = false; - dataGridView.Columns["ClientId"].Visible = false; - dataGridView.Columns["ClientEmail"].Visible = false; - dataGridView.Columns["ImplementerId"].Visible = false; - } + dataGridView.FillandConfigGrid(_orderLogic.ReadList(null)); + _logger.LogInformation("Загрузка заказов"); } catch (Exception ex) { diff --git a/FlowerShop/FlowerShopContracts/Attributes/ColumnAttribute.cs b/FlowerShop/FlowerShopContracts/Attributes/ColumnAttribute.cs new file mode 100644 index 0000000..c751046 --- /dev/null +++ b/FlowerShop/FlowerShopContracts/Attributes/ColumnAttribute.cs @@ -0,0 +1,22 @@ +namespace FlowerShopContracts.Attributes +{ + [AttributeUsage(AttributeTargets.Property)] + public class ColumnAttribute : Attribute + { + public ColumnAttribute(string title = "", bool visible = true, int width = 0, + GridViewAutoSize gridViewAutoSize = GridViewAutoSize.None, + bool isUseAutoSize = false) + { + Title = title; + Visible = visible; + Width = width; + GridViewAutoSize = gridViewAutoSize; + IsUseAutoSize = isUseAutoSize; + } + public string Title { get; private set; } + public bool Visible { get; private set; } + public int Width { get; private set; } + public GridViewAutoSize GridViewAutoSize { get; private set; } + public bool IsUseAutoSize { get; private set; } + } +} diff --git a/FlowerShop/FlowerShopContracts/Attributes/GridViewAutoSize.cs b/FlowerShop/FlowerShopContracts/Attributes/GridViewAutoSize.cs new file mode 100644 index 0000000..61caaf0 --- /dev/null +++ b/FlowerShop/FlowerShopContracts/Attributes/GridViewAutoSize.cs @@ -0,0 +1,14 @@ +namespace FlowerShopContracts.Attributes +{ + public enum GridViewAutoSize + { + NotSet = 0, + None = 1, + ColumnHeader = 2, + AllCellsExceptHeader = 4, + AllCells = 6, + DisplayedCellsExceptHeader = 8, + DisplayedCells = 10, + Fill = 16 + } +} diff --git a/FlowerShop/FlowerShopContracts/ViewModels/ClientViewModel.cs b/FlowerShop/FlowerShopContracts/ViewModels/ClientViewModel.cs index 74e3f82..bde6694 100644 --- a/FlowerShop/FlowerShopContracts/ViewModels/ClientViewModel.cs +++ b/FlowerShop/FlowerShopContracts/ViewModels/ClientViewModel.cs @@ -1,17 +1,18 @@ -using FlowerShopDataModels.Models; -using System.ComponentModel; +using FlowerShopContracts.Attributes; +using FlowerShopDataModels.Models; namespace FlowerShopContracts.ViewModels { public class ClientViewModel : IClientModel { - public int Id { get; set; } - [DisplayName("ФИО клиента")] - public string ClientFIO { get; set; } = string.Empty; - [DisplayName("Логин (эл. почта)")] - public string Email { get; set; } = string.Empty; - [DisplayName("Пароль")] - public string Password { get; set; } = string.Empty; + [Column(visible: false)] + public int Id { get; set; } + [Column(title: "ФИО клиента", width: 150)] + public string ClientFIO { get; set; } = string.Empty; + [Column(title: "Логин (эл. почта)", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] + public string Email { get; set; } = string.Empty; + [Column(title: "Пароль", width: 150)] + public string Password { get; set; } = string.Empty; } } diff --git a/FlowerShop/FlowerShopContracts/ViewModels/ComponentViewModel.cs b/FlowerShop/FlowerShopContracts/ViewModels/ComponentViewModel.cs index 2d1b5cb..e61d98e 100644 --- a/FlowerShop/FlowerShopContracts/ViewModels/ComponentViewModel.cs +++ b/FlowerShop/FlowerShopContracts/ViewModels/ComponentViewModel.cs @@ -1,16 +1,16 @@ -using FlowerShopDataModels.Models; -using System.ComponentModel; +using FlowerShopContracts.Attributes; +using FlowerShopDataModels.Models; namespace FlowerShopContracts.ViewModels { public class ComponentViewModel : IComponentModel { - public int Id { get; set; } + [Column(visible: false)] + public int Id { get; set; } - [DisplayName("Название компонента")] - public string ComponentName { get; set; } = string.Empty; - - [DisplayName("Цена")] - public double Cost { get; set; } + [Column(title: "Название компонента", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] + public string ComponentName { get; set; } = string.Empty; + [Column(title: "Цена", width: 150)] + public double Cost { get; set; } } } diff --git a/FlowerShop/FlowerShopContracts/ViewModels/FlowerViewModel.cs b/FlowerShop/FlowerShopContracts/ViewModels/FlowerViewModel.cs index fd80e8d..a054b33 100644 --- a/FlowerShop/FlowerShopContracts/ViewModels/FlowerViewModel.cs +++ b/FlowerShop/FlowerShopContracts/ViewModels/FlowerViewModel.cs @@ -1,16 +1,18 @@ -using FlowerShopDataModels.Models; -using System.ComponentModel; +using FlowerShopContracts.Attributes; +using FlowerShopDataModels.Models; namespace FlowerShopContracts.ViewModels { public class FlowerViewModel : IFlowerModel { + [Column(visible: false)] public int Id { get; set; } - [DisplayName("Название изделия")] + [Column(title: "Название изделия", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string FlowerName { get; set; } = string.Empty; - [DisplayName("Цена")] + [Column(title: "Цена", width: 150)] public double Price { get; set; } - public Dictionary FlowerComponents + [Column(visible: false)] + public Dictionary FlowerComponents { get; set; diff --git a/FlowerShop/FlowerShopContracts/ViewModels/ImplementerViewModel.cs b/FlowerShop/FlowerShopContracts/ViewModels/ImplementerViewModel.cs index c798605..a532199 100644 --- a/FlowerShop/FlowerShopContracts/ViewModels/ImplementerViewModel.cs +++ b/FlowerShop/FlowerShopContracts/ViewModels/ImplementerViewModel.cs @@ -1,18 +1,19 @@ -using FlowerShopDataModels.Models; -using System.ComponentModel; +using FlowerShopContracts.Attributes; +using FlowerShopDataModels.Models; namespace FlowerShopContracts.ViewModels { public class ImplementerViewModel : IImplementerModel { + [Column(visible: false)] public int Id { get; set; } - [DisplayName("ФИО исполнителя")] + [Column(title: "ФИО исполнителя", gridViewAutoSize: GridViewAutoSize.AllCells, isUseAutoSize: true)] public string ImplementerFIO { get; set; } = string.Empty; - [DisplayName("Пароль")] + [Column(title: "Пароль", width: 100)] public string Password { get; set; } = string.Empty; - [DisplayName("Опыт исполнителя")] + [Column(title: "Опыт исполнителя" ,gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public int WorkExperience { get; set; } - [DisplayName("Квалификация исполнителя")] + [Column(title: "Квалификация исполнителя", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public int Qualification { get; set; } } } diff --git a/FlowerShop/FlowerShopContracts/ViewModels/MessageInfoViewModel.cs b/FlowerShop/FlowerShopContracts/ViewModels/MessageInfoViewModel.cs index 0454d46..a1c82d1 100644 --- a/FlowerShop/FlowerShopContracts/ViewModels/MessageInfoViewModel.cs +++ b/FlowerShop/FlowerShopContracts/ViewModels/MessageInfoViewModel.cs @@ -1,19 +1,23 @@ -using FlowerShopDataModels.Models; -using System.ComponentModel; +using FlowerShopContracts.Attributes; +using FlowerShopDataModels.Models; namespace FlowerShopContracts.ViewModels { public class MessageInfoViewModel : IMessageInfoModel { - public string MessageId { get; set; } = string.Empty; - public int? ClientId { get; set; } - [DisplayName("Отправитель")] + [Column(visible: false)] + public int Id { get; set; } + [Column(visible: false)] + public string MessageId { get; set; } = string.Empty; + [Column(visible: false)] + public int? ClientId { get; set; } + [Column(title: "Отправитель" ,width: 150)] public string SenderName { get; set; } = string.Empty; - [DisplayName("Дата доставки")] + [Column(title: "Дата доставки", width: 120)] public DateTime DateDelivery { get; set; } - [DisplayName("Тема")] + [Column(title: "Тема", width: 120)] public string Subject { get; set; } = string.Empty; - [DisplayName("Содержание")] + [Column(title: "Содержание", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string Body { get; set; } = string.Empty; } } diff --git a/FlowerShop/FlowerShopContracts/ViewModels/OrderViewModel.cs b/FlowerShop/FlowerShopContracts/ViewModels/OrderViewModel.cs index 27000b8..f2007f6 100644 --- a/FlowerShop/FlowerShopContracts/ViewModels/OrderViewModel.cs +++ b/FlowerShop/FlowerShopContracts/ViewModels/OrderViewModel.cs @@ -1,33 +1,36 @@ -using FlowerShopDataModels.Enums; +using FlowerShopContracts.Attributes; +using FlowerShopDataModels.Enums; using FlowerShopDataModels.Models; -using System.ComponentModel; namespace FlowerShopContracts.ViewModels { public class OrderViewModel : IOrderModel { - [DisplayName("Номер")] + [Column(title: "Номер", gridViewAutoSize: GridViewAutoSize.AllCells, isUseAutoSize: true)] public int Id { get; set; } + [Column(visible: false)] public int FlowerId { get; set; } - public int ClientId { get; set; } - public int? ImplementerId { get; set; } - [DisplayName("Изделие")] + [Column(visible: false)] + public int ClientId { get; set; } + [Column(visible: false)] + public int? ImplementerId { get; set; } + [Column(title: "Изделие", gridViewAutoSize: GridViewAutoSize.AllCells, isUseAutoSize: true)] public string FlowerName { get; set; } = string.Empty; - [DisplayName("ФИО клиента")] + [Column(title: "ФИО клиента", width: 120)] public string ClientFIO { get; set; } = string.Empty; - [DisplayName("Почта клиента")] + [Column(title: "Почта клиента", width: 190] public string ClientEmail { get; set; } = string.Empty; - [DisplayName("ФИО исполнителя")] + [Column(title: "ФИО исполнителя", width: 120)] public string ImplementerFIO { get; set; }=string.Empty; - [DisplayName("Количество")] + [Column(title: "Количество", width: 100)] public int Count { get; set; } - [DisplayName("Сумма")] + [Column(title: "Сумма", width: 75)] public double Sum { get; set; } - [DisplayName("Статус")] + [Column(title: "Статус", width:70)] public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - [DisplayName("Дата создания")] + [Column(title: "Дата создания", width: 120)] public DateTime DateCreate { get; set; } = DateTime.Now; - [DisplayName("Дата выполнения")] + [Column(title: "Дата выполнения", width:120)] public DateTime? DateImplement { get; set; } } }