Old DataGrid filling methods have been replaced by FillAndConfigGrid.

This commit is contained in:
Yuee Shiness 2023-04-23 14:46:17 +04:00
parent d0d4333580
commit 5ed0dc584b
7 changed files with 53 additions and 49 deletions

View File

@ -0,0 +1,47 @@
using DressAtelierContracts.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SewingDresses
{
public static class DataGridViewExtension
{
public static void FillAndConfigGrid<T>(this DataGridView grid, List<T>? 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($"In type {type.Name} property with name {column.Name} wasn't found");
}
var attribute = property.GetCustomAttributes(typeof(ColumnAttribute), true)?.SingleOrDefault();
if (attribute == null)
{
throw new InvalidOperationException($"Attribute of type ColumnAttribute for property {property.Name} wasn't found");
}
// attempt to find proper attribute
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;
}
}
}
}
}
}

View File

@ -62,16 +62,7 @@ namespace SewingDresses
{ {
try try
{ {
var list = _logic.ReadList(null); clientGridView.FillAndConfigGrid(_logic.ReadList(null));
if (list != null)
{
clientGridView.DataSource = list;
clientGridView.Columns["ID"].Visible = false;
clientGridView.Columns["FullName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
clientGridView.Columns["Email"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
clientGridView.Columns["Password"].Visible = false;
}
_logger.LogInformation("Loading clients"); _logger.LogInformation("Loading clients");
} }
catch (Exception ex) catch (Exception ex)

View File

@ -34,15 +34,7 @@ namespace SewingDresses
{ {
try try
{ {
var list = _logic.ReadList(null); dressGridView.FillAndConfigGrid(_logic.ReadList(null));
if (list != null)
{
dressGridView.DataSource = list;
dressGridView.Columns["ID"].Visible = false;
dressGridView.Columns["DressName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dressGridView.Columns["DressComponents"].Visible = false;
}
_logger.LogInformation("Loading dresses"); _logger.LogInformation("Loading dresses");
} }
catch (Exception ex) catch (Exception ex)

View File

@ -33,14 +33,7 @@ namespace SewingDresses
{ {
try try
{ {
var list = _logic.ReadList(null); emailsGridView.FillAndConfigGrid(_logic.ReadList(null));
if (list != null)
{
emailsGridView.DataSource = list;
emailsGridView.Columns["ClientID"].Visible = false;
emailsGridView.Columns["ID"].Visible = false;
emailsGridView.Columns["Body"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
_logger.LogInformation("Loading materials"); _logger.LogInformation("Loading materials");
} }
catch (Exception ex) catch (Exception ex)

View File

@ -62,13 +62,7 @@ namespace SewingDresses
{ {
try try
{ {
var list = _logic.ReadList(null); employeeGridView.FillAndConfigGrid(_logic.ReadList(null));
if (list != null)
{
employeeGridView.DataSource = list;
employeeGridView.Columns["ID"].Visible = false;
employeeGridView.Columns["FullName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
_logger.LogInformation("Loading employees"); _logger.LogInformation("Loading employees");
} }
catch (Exception ex) catch (Exception ex)

View File

@ -41,14 +41,7 @@ namespace SewingDresses
_logger.LogInformation("Downloading orders"); _logger.LogInformation("Downloading orders");
try try
{ {
var list = _orderLogic.ReadList(null); dataGridView.FillAndConfigGrid(_orderLogic.ReadList(null));
if (list != null)
{
dataGridView.DataSource = list;
dataGridView.Columns["DressID"].Visible = false;
dataGridView.Columns["ClientID"].Visible = false;
dataGridView.Columns["EmployeeID"].Visible = false;
}
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -35,13 +35,7 @@ namespace SewingDresses
{ {
try try
{ {
var list = _logic.ReadList(null); materialGridView.FillAndConfigGrid(_logic.ReadList(null));
if (list != null)
{
materialGridView.DataSource = list;
materialGridView.Columns["ID"].Visible = false;
materialGridView.Columns["ComponentName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
_logger.LogInformation("Loading materials"); _logger.LogInformation("Loading materials");
} }
catch (Exception ex) catch (Exception ex)