diff --git a/Confectionery/ConfectioneryView/DataGridViewExtension.cs b/Confectionery/ConfectioneryView/DataGridViewExtension.cs index a0f05e5..4099850 100644 --- a/Confectionery/ConfectioneryView/DataGridViewExtension.cs +++ b/Confectionery/ConfectioneryView/DataGridViewExtension.cs @@ -3,10 +3,49 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using ConfectioneryContracts.Attributes; namespace ConfectioneryView { - internal class DataGridViewExtension + 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/Confectionery/ConfectioneryView/FormClients.cs b/Confectionery/ConfectioneryView/FormClients.cs index a93ec53..af4c195 100644 --- a/Confectionery/ConfectioneryView/FormClients.cs +++ b/Confectionery/ConfectioneryView/FormClients.cs @@ -35,14 +35,8 @@ namespace ConfectioneryView { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["ClientFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } - _logger.LogInformation("Загрузка списка клиентов"); + dataGridView.FillAndConfigGrid(_logic.ReadList(null)); + _logger.LogInformation("Загрузка списка клиентов"); } catch (Exception ex) { diff --git a/Confectionery/ConfectioneryView/FormComponents.cs b/Confectionery/ConfectioneryView/FormComponents.cs index fb3ad31..0c895ba 100644 --- a/Confectionery/ConfectioneryView/FormComponents.cs +++ b/Confectionery/ConfectioneryView/FormComponents.cs @@ -44,15 +44,8 @@ namespace ConfectioneryView { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["ComponentName"].AutoSizeMode = - DataGridViewAutoSizeColumnMode.Fill; - } - _logger.LogInformation("Загрузка компонентов"); + dataGridView.FillAndConfigGrid(_logic.ReadList(null)); + _logger.LogInformation("Загрузка компонентов"); } catch (Exception ex) { diff --git a/Confectionery/ConfectioneryView/FormImplementers.cs b/Confectionery/ConfectioneryView/FormImplementers.cs index be8dafb..9906622 100644 --- a/Confectionery/ConfectioneryView/FormImplementers.cs +++ b/Confectionery/ConfectioneryView/FormImplementers.cs @@ -29,14 +29,7 @@ namespace ConfectioneryView { 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/Confectionery/ConfectioneryView/FormMail.cs b/Confectionery/ConfectioneryView/FormMail.cs index 3def25c..e823f99 100644 --- a/Confectionery/ConfectioneryView/FormMail.cs +++ b/Confectionery/ConfectioneryView/FormMail.cs @@ -28,15 +28,7 @@ namespace ConfectioneryView { try { - var list = _logic.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(_logic.ReadList(null)); _logger.LogInformation("Загрузка почтовых собщений"); } catch (Exception ex) diff --git a/Confectionery/ConfectioneryView/FormMain.cs b/Confectionery/ConfectioneryView/FormMain.cs index 03aa49d..8498549 100644 --- a/Confectionery/ConfectioneryView/FormMain.cs +++ b/Confectionery/ConfectioneryView/FormMain.cs @@ -37,17 +37,7 @@ namespace ConfectioneryView _logger.LogInformation("Загрузка заказов"); try { - var list = _orderLogic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["PastryId"].Visible = false; - dataGridView.Columns["ClientId"].Visible = false; - dataGridView.Columns["ClientEmail"].Visible = false; - dataGridView.Columns["ImplementerId"].Visible = false; - dataGridView.Columns["PastryName"].AutoSizeMode = - DataGridViewAutoSizeColumnMode.Fill; - } + dataGridView.FillAndConfigGrid(_orderLogic.ReadList(null)); _logger.LogInformation("Загрузка заказов"); } catch (Exception ex) diff --git a/Confectionery/ConfectioneryView/FormPastrys.cs b/Confectionery/ConfectioneryView/FormPastrys.cs index c416875..09f2aea 100644 --- a/Confectionery/ConfectioneryView/FormPastrys.cs +++ b/Confectionery/ConfectioneryView/FormPastrys.cs @@ -32,16 +32,8 @@ namespace ConfectioneryView { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["PastryComponents"].Visible = false; - dataGridView.Columns["PastryName"].AutoSizeMode = - DataGridViewAutoSizeColumnMode.Fill; - } - _logger.LogInformation("Загрузка кондитерского изделия"); + dataGridView.FillAndConfigGrid(_logic.ReadList(null)); + _logger.LogInformation("Загрузка кондитерского изделия"); } catch (Exception ex) {