Изменения на лабе

This commit is contained in:
bekodeg 2024-09-17 11:03:04 +04:00
parent 75ea67124a
commit 82b493f7aa
4 changed files with 56 additions and 37 deletions

View File

@ -2,38 +2,46 @@
namespace Cop.Borovkov.Var3.Components
{
/// <summary>
/// Визуальный компонент вывода таблицы значений
/// </summary>
public partial class CustomDataTable : UserControl
{
private readonly List<string> _columnsFildsNames = new();
/// <summary>
/// </summary>
public CustomDataTable()
{
InitializeComponent();
}
/// <summary>
/// Определить структуру таблицы
/// </summary>
/// <param name="columnParameters"></param>
public void ConfigureColumns(params CustomDataTableColumnParameter[] columnParameters)
{
outDataGridView.Columns.Clear();
_columnsFildsNames.Clear();
for (int i = 0; i < columnParameters.Length; i++)
foreach (var columnParametr in columnParameters)
{
outDataGridView.Columns
.Add(columnParameters[i].HeaderName, columnParameters[i].HeaderName);
var column = outDataGridView.Columns[i];
column.Visible = columnParameters[i].Visible;
_columnsFildsNames.Add(column.Name);
if (columnParameters[i].Width != 0)
DataGridViewTextBoxColumn column = new()
{
column.Width = columnParameters[i].Width;
Name = columnParametr.HeaderName,
HeaderText = columnParametr.HeaderName,
DataPropertyName = columnParametr.PropertyName,
Visible = columnParametr.Visible,
};
if (columnParametr.Width != 0)
{
column.Width = columnParametr.Width;
}
else
{
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
}
outDataGridView.Columns.Add(column);
}
}
@ -57,10 +65,16 @@ namespace Cop.Borovkov.Var3.Components
}
}
public TType? GetRow<TType>(int rowIndex)
/// <summary>
/// Получить стоку таблицы в виде объекта
/// </summary>
/// <typeparam name="TType"></typeparam>
/// <param name="rowIndex"></param>
/// <returns></returns>
public TType? GetRow<TType>(int rowIndex) where TType : new()
{
Type type = typeof(TType);
var element = type.GetConstructor([])?.Invoke([]);
var element = new TType();
if (element is null)
{
@ -69,19 +83,25 @@ namespace Cop.Borovkov.Var3.Components
var row = outDataGridView.Rows[rowIndex].Cells;
for (int i = 0; i < outDataGridView.ColumnCount; i++)
for (int i = 0; i < row.Count; i++)
{
if (string.IsNullOrEmpty(_columnsFildsNames[i]))
if (string.IsNullOrEmpty(row[i].OwningColumn.DataPropertyName))
{
continue;
}
var field = type.GetProperty(_columnsFildsNames[i]);
var field = type.GetProperty(row[i].OwningColumn.DataPropertyName);
field?.SetValue(element, row[i].Value);
}
return (TType)element;
return element;
}
/// <summary>
/// Заполнить таблицу
/// </summary>
/// <typeparam name="TType"></typeparam>
/// <param name="insertValues"></param>
public void Fill<TType>(IList<TType> insertValues)
{
Clear();
@ -93,10 +113,10 @@ namespace Cop.Borovkov.Var3.Components
var row = insertValues[i];
outDataGridView.Rows.Add();
for (int j = 0; j < _columnsFildsNames.Count; ++j)
for (int j = 0; j < outDataGridView.ColumnCount; ++j)
{
outDataGridView.Rows[i].Cells[j].Value
= type.GetProperty(_columnsFildsNames[j])?.GetValue(row)!;
= type.GetProperty(outDataGridView.Columns[j].DataPropertyName)?.GetValue(row)!;
}
}
}

View File

@ -1,5 +1,8 @@
namespace Cop.Borovkov.Var3.Components
{
/// <summary>
/// Визуальный компонент выбора из списка значений
/// </summary>
public partial class CustomListBox : UserControl
{
private event EventHandler? _selectedChanged;

View File

@ -51,7 +51,7 @@
inputField.Name = "inputField";
inputField.Size = new Size(173, 23);
inputField.TabIndex = 1;
inputField.TextChanged += inputField_TextChanged;
inputField.TextChanged += InputField_TextChanged;
//
// CustomNumericInputField
//

View File

@ -2,6 +2,9 @@
namespace Cop.Borovkov.Var3.Components
{
/// <summary>
/// Визуальный компонент ввода целочисленного значения допускающего null
/// </summary>
public partial class CustomNumericInputField : UserControl
{
private event EventHandler? _numericInputChanged;
@ -34,7 +37,7 @@ namespace Cop.Borovkov.Var3.Components
}
/// <summary>
/// Значения поля
/// Значения поля ввода
/// </summary>
public int? Value
{
@ -54,13 +57,10 @@ namespace Cop.Borovkov.Var3.Components
}
set
{
if (value is null)
isNullcheckBox.Checked = value is null;
if (!isNullcheckBox.Checked)
{
isNullcheckBox.Checked = true;
}
else
{
isNullcheckBox.Checked = false;
inputField.Text = value.ToString();
}
}
@ -68,14 +68,10 @@ namespace Cop.Borovkov.Var3.Components
private void IsNullcheckBox_CheckedChanged(object sender, EventArgs e)
{
if (isNullcheckBox.Checked)
inputField.Enabled = !isNullcheckBox.Checked;
if (!inputField.Enabled)
{
inputField.Text = string.Empty;
inputField.Enabled = false;
}
else
{
inputField.Enabled = true;
}
try
@ -88,7 +84,7 @@ namespace Cop.Borovkov.Var3.Components
}
}
private void inputField_TextChanged(object sender, EventArgs e)
private void InputField_TextChanged(object sender, EventArgs e)
{
try
{