diff --git a/FurnitureAssembly/FurnitureAssembly/DataGridViewExtension.cs b/FurnitureAssembly/FurnitureAssembly/DataGridViewExtension.cs new file mode 100644 index 0000000..6952f2e --- /dev/null +++ b/FurnitureAssembly/FurnitureAssembly/DataGridViewExtension.cs @@ -0,0 +1,51 @@ +using FurnitureAssemblyContracts.Attributes; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssembly +{ + 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/FurnitureAssembly/FurnitureAssembly/FormClients.cs b/FurnitureAssembly/FurnitureAssembly/FormClients.cs index 1d18bca..c293ba3 100644 --- a/FurnitureAssembly/FurnitureAssembly/FormClients.cs +++ b/FurnitureAssembly/FurnitureAssembly/FormClients.cs @@ -1,4 +1,5 @@ -using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyBusinessLogic; +using FurnitureAssemblyContracts.BindingModels; using FurnitureAssemblyContracts.BusinessLogicsContarcts; using Microsoft.Extensions.Logging; @@ -24,17 +25,7 @@ namespace FurnitureAssembly { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["ClientFIO"].AutoSizeMode = - DataGridViewAutoSizeColumnMode.Fill; - dataGridView.Columns["Email"].AutoSizeMode = - DataGridViewAutoSizeColumnMode.Fill; - dataGridView.Columns["Password"].Visible = false; - } + dataGridView.FillandConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Загрузка клиентов"); } catch (Exception ex) diff --git a/FurnitureAssembly/FurnitureAssembly/FormComponents.cs b/FurnitureAssembly/FurnitureAssembly/FormComponents.cs index ea435f5..1c1e4db 100644 --- a/FurnitureAssembly/FurnitureAssembly/FormComponents.cs +++ b/FurnitureAssembly/FurnitureAssembly/FormComponents.cs @@ -1,4 +1,5 @@ -using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyBusinessLogic; +using FurnitureAssemblyContracts.BindingModels; using FurnitureAssemblyContracts.BusinessLogicsContarcts; using Microsoft.Extensions.Logging; using System; @@ -34,14 +35,7 @@ namespace FurnitureAssembly { 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/FurnitureAssembly/FurnitureAssembly/FormFurnitures.cs b/FurnitureAssembly/FurnitureAssembly/FormFurnitures.cs index ad82020..0d33300 100644 --- a/FurnitureAssembly/FurnitureAssembly/FormFurnitures.cs +++ b/FurnitureAssembly/FurnitureAssembly/FormFurnitures.cs @@ -1,4 +1,5 @@ -using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyBusinessLogic; +using FurnitureAssemblyContracts.BindingModels; using FurnitureAssemblyContracts.BusinessLogicsContarcts; using Microsoft.Extensions.Logging; using System; @@ -33,15 +34,7 @@ namespace FurnitureAssembly { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["FurnitureName"].AutoSizeMode = - DataGridViewAutoSizeColumnMode.Fill; - dataGridView.Columns["FurnitureComponents"].Visible = false; - } + dataGridView.FillandConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Загрузка изделий"); } catch (Exception ex) diff --git a/FurnitureAssembly/FurnitureAssembly/FormImplementers.cs b/FurnitureAssembly/FurnitureAssembly/FormImplementers.cs index df5aa63..2d177cf 100644 --- a/FurnitureAssembly/FurnitureAssembly/FormImplementers.cs +++ b/FurnitureAssembly/FurnitureAssembly/FormImplementers.cs @@ -1,4 +1,5 @@ -using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyBusinessLogic; +using FurnitureAssemblyContracts.BindingModels; using FurnitureAssemblyContracts.BusinessLogicsContarcts; using Microsoft.Extensions.Logging; @@ -25,14 +26,7 @@ namespace FurnitureAssembly { 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/FurnitureAssembly/FurnitureAssembly/FormMail.cs b/FurnitureAssembly/FurnitureAssembly/FormMail.cs index fd2afdb..52c8e9e 100644 --- a/FurnitureAssembly/FurnitureAssembly/FormMail.cs +++ b/FurnitureAssembly/FurnitureAssembly/FormMail.cs @@ -1,4 +1,5 @@ -using FurnitureAssemblyContracts.BusinessLogicsContarcts; +using FurnitureAssemblyBusinessLogic; +using FurnitureAssemblyContracts.BusinessLogicsContarcts; using Microsoft.Extensions.Logging; using System.Windows.Forms; @@ -20,14 +21,7 @@ namespace FurnitureAssembly { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridViewMail.DataSource = list; - dataGridViewMail.Columns["ClientId"].Visible = false; - dataGridViewMail.Columns["MessageId"].Visible = false; - dataGridViewMail.Columns["Body"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } + dataGridViewMail.FillandConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Загрузка писем"); } catch (Exception ex) diff --git a/FurnitureAssembly/FurnitureAssembly/FormMain.cs b/FurnitureAssembly/FurnitureAssembly/FormMain.cs index 21806d5..d47b0eb 100644 --- a/FurnitureAssembly/FurnitureAssembly/FormMain.cs +++ b/FurnitureAssembly/FurnitureAssembly/FormMain.cs @@ -36,14 +36,7 @@ namespace FurnitureAssembly { try { - var list = _orderLogic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["FurnitureId"].Visible = false; - dataGridView.Columns["ClientId"].Visible = false; - dataGridView.Columns["ImplementerId"].Visible = false; - } + dataGridView.FillandConfigGrid(_orderLogic.ReadList(null)); _logger.LogInformation("Загрузка заказов"); } catch (Exception ex) diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/Attributes/ColumnAttribute.cs b/FurnitureAssembly/FurnitureAssemblyContracts/Attributes/ColumnAttribute.cs new file mode 100644 index 0000000..f453606 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyContracts/Attributes/ColumnAttribute.cs @@ -0,0 +1,24 @@ +using System; + +namespace FurnitureAssemblyContracts.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/FurnitureAssembly/FurnitureAssemblyContracts/Attributes/GridViewAutoSize.cs b/FurnitureAssembly/FurnitureAssemblyContracts/Attributes/GridViewAutoSize.cs new file mode 100644 index 0000000..722d1bb --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyContracts/Attributes/GridViewAutoSize.cs @@ -0,0 +1,14 @@ +namespace FurnitureAssemblyContracts.Attributes +{ + public enum GridViewAutoSize + { + NotSet = 0, + None = 1, + ColumnHeader = 2, + AllCellsExceptHeader = 4, + AllCells = 6, + DisplayedCellsExceptHeader = 8, + DisplayedCells = 10, + Fill = 16 + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ClientViewModel.cs b/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ClientViewModel.cs index 76344c0..585f75c 100644 --- a/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ClientViewModel.cs +++ b/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ClientViewModel.cs @@ -1,17 +1,18 @@ -using FurnitureAssemblyDataModels.Models; -using System.ComponentModel; +using FurnitureAssemblyContracts.Attributes; +using FurnitureAssemblyDataModels.Models; namespace FurnitureAssemblyContracts.ViewModels { public class ClientViewModel : IClientModel { + [Column(visible: false)] public int Id { get; set; } - [DisplayName("ФИО клиента")] + [Column(title: "ФИО клиента", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true )] public string ClientFIO { get; set; } = string.Empty; - [DisplayName("Логин (эл. почта)")] + [Column(title: "Логин (эл. почта)", width: 150)] public string Email { get; set; } = string.Empty; - [DisplayName("Пароль")] + [Column(title: "Пароль", width: 150)] public string Password { get; set; } = string.Empty; } } diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ComponentViewModel.cs b/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ComponentViewModel.cs index 516ff28..88c8df3 100644 --- a/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ComponentViewModel.cs +++ b/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ComponentViewModel.cs @@ -1,19 +1,16 @@ -using FurnitureAssemblyDataModels.Models; -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using FurnitureAssemblyContracts.Attributes; +using FurnitureAssemblyDataModels.Models; + namespace FurnitureAssemblyContracts.ViewModels { public class ComponentViewModel : IComponentModel { + [Column(visible: false)] public int Id { get; set; } - [DisplayName("Название компонента")] + [Column(title: "Название компонента", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string ComponentName { get; set; } = string.Empty; - [DisplayName("Цена")] + [Column(title: "Цена", width: 70)] public double Cost { get; set; } } } diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/FurnitureViewModel.cs b/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/FurnitureViewModel.cs index ea144c1..9dd4ce6 100644 --- a/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/FurnitureViewModel.cs +++ b/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/FurnitureViewModel.cs @@ -1,7 +1,7 @@ -using FurnitureAssemblyDataModels.Models; +using FurnitureAssemblyContracts.Attributes; +using FurnitureAssemblyDataModels.Models; using System; using System.Collections.Generic; -using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -10,11 +10,13 @@ namespace FurnitureAssemblyContracts.ViewModels { public class FurnitureViewModel : IFurnitureModel { + [Column(visible: false)] public int Id { get; set; } - [DisplayName("Название изделия")] + [Column(title: "Название изделия", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string FurnitureName { get; set; } = string.Empty; - [DisplayName("Цена")] + [Column(title: "Цена", width: 70)] public double Price { get; set; } + [Column(visible: false)] public Dictionary FurnitureComponents{ get; set; } = new(); } } diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ImplementerViewModel.cs b/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ImplementerViewModel.cs index 198964e..3d0e32a 100644 --- a/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ImplementerViewModel.cs +++ b/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ImplementerViewModel.cs @@ -1,7 +1,7 @@ -using FurnitureAssemblyDataModels.Models; +using FurnitureAssemblyContracts.Attributes; +using FurnitureAssemblyDataModels.Models; using System; using System.Collections.Generic; -using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -9,15 +9,16 @@ using System.Threading.Tasks; namespace FurnitureAssemblyContracts.ViewModels { public class ImplementerViewModel : IImplementerModel - { + { + [Column(visible: false)] public int Id { get; set; } - [DisplayName("ФИО исполнителя")] + [Column(title: "ФИО исполнителя", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string ImplementerFIO { get; set; } = string.Empty; - [DisplayName("Пароль")] + [Column(title: "Пароль", width: 150)] public string Password { get; set; } = string.Empty; - [DisplayName("Опыт работы")] + [Column(title: "Опыт работы", gridViewAutoSize: GridViewAutoSize.AllCells, isUseAutoSize: true)] public int WorkExperience { get; set; } - [DisplayName("Квалификация")] + [Column(title: "Квалификация", gridViewAutoSize: GridViewAutoSize.AllCells, isUseAutoSize: true)] public int Qualification { get; set; } } } diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/MessageInfoViewModel.cs b/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/MessageInfoViewModel.cs index 98b85aa..5a90aac 100644 --- a/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/MessageInfoViewModel.cs +++ b/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/MessageInfoViewModel.cs @@ -1,20 +1,22 @@ -using FurnitureAssemblyDataModels.Models; -using System.ComponentModel; +using FurnitureAssemblyContracts.Attributes; +using FurnitureAssemblyDataModels.Models; + namespace FurnitureAssemblyContracts.ViewModels { public class MessageInfoViewModel : IMessageInfoModel { + [Column(visible: false)] public string MessageId { get; set; } = string.Empty; - + [Column(visible: false)] public int? ClientId { get; set; } - [DisplayName("Отправитель")] + [Column(title: "Отправитель", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string SenderName { get; set; } = string.Empty; - [DisplayName("Дата письма")] + [Column(title: "Дата письма", width: 100)] public DateTime DateDelivery { get; set; } - [DisplayName("Заголовок")] - public string Subject { get; set; } = string.Empty; - [DisplayName("Текст")] + [Column(title: "Заголовок", width: 170)] + public string Subject { get; set; } = string.Empty; + [Column(title: "Текст", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string Body { get; set; } = string.Empty; } } diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/OrderViewModel.cs b/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/OrderViewModel.cs index f2fb1e3..f8ff5de 100644 --- a/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/OrderViewModel.cs +++ b/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/OrderViewModel.cs @@ -1,8 +1,8 @@ -using FurnitureAssemblyDataModels.Enums; +using FurnitureAssemblyContracts.Attributes; +using FurnitureAssemblyDataModels.Enums; using FurnitureAssemblyDataModels.Models; using System; using System.Collections.Generic; -using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -11,26 +11,29 @@ namespace FurnitureAssemblyContracts.ViewModels { public class OrderViewModel : IOrderModel { - [DisplayName("Номер")] + [Column(title: "Номер", width: 50)] public int Id { get; set; } + [Column(visible: false)] public int ClientId { get; set; } - [DisplayName("Клиент")] + [Column(title: "Клиент", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string ClientFIO { get; set; } = string.Empty; + [Column(visible: false)] public int FurnitureId { get; set; } - [DisplayName("Изделие")] + [Column(title: "Изделие", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string FurnitureName { get; set; } = string.Empty; - [DisplayName("Количество")] + [Column(title: "Количество", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public int Count { get; set; } - [DisplayName("Сумма")] + [Column(title: "Сумма", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public double Sum { get; set; } - [DisplayName("Статус")] + [Column(title: "Статус", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - [DisplayName("Дата создания")] + [Column(title: "Дата создания", width: 100)] public DateTime DateCreate { get; set; } = DateTime.Now; - [DisplayName("Дата выполнения")] + [Column(title: "Дата выполнения", width: 100)] public DateTime? DateImplement { get; set; } + [Column(visible: false)] public int? ImplementerId { get; set; } - [DisplayName("Исполнитель")] + [Column(title: "Исполнитель", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string? ImplementerFIO { get; set; } = string.Empty; } }