diff --git a/SushiBar/Components/FormComponents.cs b/SushiBar/Components/FormComponents.cs index 5c496d9..59f769c 100644 --- a/SushiBar/Components/FormComponents.cs +++ b/SushiBar/Components/FormComponents.cs @@ -2,15 +2,7 @@ using SushiBar; using SushiBarContracts.BindingModel; using SushiBarContracts.BusinessLogicsContracts; -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; +using SushiBarContracts.ViewModels; namespace SushiBarView { @@ -38,13 +30,7 @@ namespace SushiBarView { 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/SushiBar/DataGridViewExtension.cs b/SushiBar/DataGridViewExtension.cs new file mode 100644 index 0000000..35346dd --- /dev/null +++ b/SushiBar/DataGridViewExtension.cs @@ -0,0 +1,46 @@ +using SushiBarContracts.Attributes; + +namespace SushiBarContracts.ViewModels +{ + 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/SushiBar/FormClients.cs b/SushiBar/FormClients.cs index 09cc9d0..56a8fc7 100644 --- a/SushiBar/FormClients.cs +++ b/SushiBar/FormClients.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.Logging; using SushiBarContracts.BindingModel; using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.ViewModels; using System; using System.Collections.Generic; using System.ComponentModel; @@ -33,14 +34,8 @@ namespace SushiBarView { try { - var list = _clientLogic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["ClientFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - _logger.LogInformation("Клиенты успешно загружены"); - } + dataGridView.FillAndConfigGrid(_clientLogic.ReadList(null)); + _logger.LogInformation("Клиенты успешно загружены"); } catch (Exception ex) { diff --git a/SushiBar/FormImplementers.cs b/SushiBar/FormImplementers.cs index 6519eee..dd45871 100644 --- a/SushiBar/FormImplementers.cs +++ b/SushiBar/FormImplementers.cs @@ -2,6 +2,7 @@ using SushiBar; using SushiBarContracts.BindingModel; using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.ViewModels; using System; using System.Collections.Generic; using System.ComponentModel; @@ -57,13 +58,7 @@ namespace SushiBarView 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/SushiBar/FormMails.cs b/SushiBar/FormMails.cs index 2d64b6f..1d59714 100644 --- a/SushiBar/FormMails.cs +++ b/SushiBar/FormMails.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.Logging; using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.ViewModels; namespace SushiBarView { @@ -18,14 +19,7 @@ namespace SushiBarView { try { - var list = messageInfoLogic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["ClientId"].Visible = false; - dataGridView.Columns["MessageId"].Visible = false; - dataGridView.Columns["Body"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } + dataGridView.FillAndConfigGrid(messageInfoLogic.ReadList(null)); logger.LogInformation("Load list of mails"); } diff --git a/SushiBar/FormMain.cs b/SushiBar/FormMain.cs index edcb1d9..69001eb 100644 --- a/SushiBar/FormMain.cs +++ b/SushiBar/FormMain.cs @@ -2,6 +2,7 @@ using SushiBar; using SushiBarContracts.BindingModel; using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.ViewModels; using SushiBarView.Reports; namespace SushiBarView @@ -22,7 +23,6 @@ namespace SushiBarView } private void FormMain_Load(object sender, EventArgs e) { - //this.WindowState = FormWindowState.Maximized; dataGridView1.Width = this.Width - 40; LoadData(); } private void FormMain_Resize(object sender, EventArgs e) @@ -33,18 +33,7 @@ namespace SushiBarView { try { - var list = _orderLogic.ReadList(null); - - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["SushiId"].Visible = false; - dataGridView.Columns["SushiName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - dataGridView.Columns["ClientId"].Visible = false; - dataGridView.Columns["ClientFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - dataGridView.Columns["ImplementerId"].Visible = false; - dataGridView.Columns["ImplementerFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } + dataGridView.FillAndConfigGrid(_orderLogic.ReadList(null)); _logger.LogInformation("Загрузка заказов"); } catch (Exception ex) diff --git a/SushiBar/Sushis/FormSushis.cs b/SushiBar/Sushis/FormSushis.cs index a8098d5..193cfb6 100644 --- a/SushiBar/Sushis/FormSushis.cs +++ b/SushiBar/Sushis/FormSushis.cs @@ -2,6 +2,7 @@ using SushiBar; using SushiBarContracts.BindingModel; using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.ViewModels; using System; using System.Collections.Generic; using System.ComponentModel; @@ -38,14 +39,7 @@ namespace SushiBarView { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["SushiName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - dataGridView.Columns["SushiComponents"].Visible = false; - } + dataGridView.FillAndConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Загрузка списка суш"); } catch (Exception ex) diff --git a/SushiBarContracts/Attributes/ColumnAttribute.cs b/SushiBarContracts/Attributes/ColumnAttribute.cs index c893986..ef8a424 100644 --- a/SushiBarContracts/Attributes/ColumnAttribute.cs +++ b/SushiBarContracts/Attributes/ColumnAttribute.cs @@ -9,9 +9,8 @@ namespace SushiBarContracts.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) + public ColumnAttribute(string title = "", bool visible = true, int width = 0, + GridViewAutoSize gridViewAutoSize = GridViewAutoSize.None, bool isUseAutoSize = false) { Title = title; Visible = visible;