From aeda3cde885cb7e6bbe40c2ec6f5e7a4dbb4d170 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=BA=20=D0=98=D0=B3=D0=BE=D1=80=D1=8C?= Date: Mon, 8 May 2023 23:31:30 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=80=D0=B8=D0=BC=D0=B5=D1=80=20=D0=BD?= =?UTF-8?q?=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B9=D0=BA=D0=B8=20datagridview?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataGridViewExtension.cs | 45 +++++++++++++++++++ .../BlacksmithWorkshop/FormClients.cs | 8 +--- .../Attributes/ColumnAttribute.cs | 20 +++++++++ .../Attributes/GridViewAutoSize.cs | 14 ++++++ .../ViewModels/ClientViewModel.cs | 13 +++--- 5 files changed, 87 insertions(+), 13 deletions(-) create mode 100644 BlacksmithWorkshop/BlacksmithWorkshop/DataGridViewExtension.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopContracts/Attributes/ColumnAttribute.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopContracts/Attributes/GridViewAutoSize.cs diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/DataGridViewExtension.cs b/BlacksmithWorkshop/BlacksmithWorkshop/DataGridViewExtension.cs new file mode 100644 index 0000000..1da18aa --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshop/DataGridViewExtension.cs @@ -0,0 +1,45 @@ +using BlacksmithWorkshopContracts.Attributes; + +namespace BlacksmithWorkshopView +{ + internal 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/BlacksmithWorkshop/BlacksmithWorkshop/FormClients.cs b/BlacksmithWorkshop/BlacksmithWorkshop/FormClients.cs index b81aff1..cd7b768 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshop/FormClients.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormClients.cs @@ -22,13 +22,7 @@ namespace BlacksmithWorkshopView { 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/BlacksmithWorkshop/BlacksmithWorkshopContracts/Attributes/ColumnAttribute.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/Attributes/ColumnAttribute.cs new file mode 100644 index 0000000..68c8093 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/Attributes/ColumnAttribute.cs @@ -0,0 +1,20 @@ +namespace BlacksmithWorkshopContracts.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/BlacksmithWorkshop/BlacksmithWorkshopContracts/Attributes/GridViewAutoSize.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/Attributes/GridViewAutoSize.cs new file mode 100644 index 0000000..797be4d --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/Attributes/GridViewAutoSize.cs @@ -0,0 +1,14 @@ +namespace BlacksmithWorkshopContracts.Attributes +{ + public enum GridViewAutoSize + { + NotSet = 0, + None = 1, + ColumnHeader = 2, + AllCellsExceptHeader = 4, + AllCells = 6, + DisplayedCellsExceptHeader = 8, + DisplayedCells = 10, + Fill = 16 + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/ClientViewModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/ClientViewModel.cs index e6dbc79..2aee158 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/ClientViewModel.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/ClientViewModel.cs @@ -1,16 +1,17 @@ -using BlacksmithWorkshopDataModels.Models; -using System.ComponentModel; +using BlacksmithWorkshopContracts.Attributes; +using BlacksmithWorkshopDataModels.Models; namespace BlacksmithWorkshopContracts.ViewModels { public class ClientViewModel : IClientModel { + [Column(visible: false)] public int Id { get; set; } - [DisplayName("ФИО клиента")] + [Column(title: "ФИО клиента", width: 150)] public string ClientFIO { get; set; } = string.Empty; - [DisplayName("Логин (эл. почта)")] + [Column(title: "Логин (эл. почта)", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string Email { get; set; } = string.Empty; - [DisplayName("Пароль")] - public string Password { get; set; } = string.Empty; + [Column(title: "Пароль", width: 150)] + public string Password { get; set; } = string.Empty; } }