LABWORK-1 #1

Merged
bekodeg merged 5 commits from LABWORK-1 into dev 2024-09-17 11:06:41 +04:00
4 changed files with 56 additions and 37 deletions
Showing only changes of commit 82b493f7aa - Show all commits

View File

@ -2,38 +2,46 @@
namespace Cop.Borovkov.Var3.Components namespace Cop.Borovkov.Var3.Components
{ {
/// <summary>
/// Визуальный компонент вывода таблицы значений
/// </summary>
public partial class CustomDataTable : UserControl public partial class CustomDataTable : UserControl
{ {
private readonly List<string> _columnsFildsNames = new(); /// <summary>
/// </summary>
public CustomDataTable() public CustomDataTable()
{ {
InitializeComponent(); InitializeComponent();
} }
/// <summary>
/// Определить структуру таблицы
/// </summary>
/// <param name="columnParameters"></param>
public void ConfigureColumns(params CustomDataTableColumnParameter[] columnParameters) public void ConfigureColumns(params CustomDataTableColumnParameter[] columnParameters)
{ {
outDataGridView.Columns.Clear(); outDataGridView.Columns.Clear();
_columnsFildsNames.Clear();
for (int i = 0; i < columnParameters.Length; i++) foreach (var columnParametr in columnParameters)
{ {
outDataGridView.Columns DataGridViewTextBoxColumn column = new()
.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)
{ {
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 else
{ {
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells; 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); Type type = typeof(TType);
var element = type.GetConstructor([])?.Invoke([]); var element = new TType();
if (element is null) if (element is null)
{ {
@ -69,19 +83,25 @@ namespace Cop.Borovkov.Var3.Components
var row = outDataGridView.Rows[rowIndex].Cells; 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; continue;
} }
var field = type.GetProperty(_columnsFildsNames[i]);
var field = type.GetProperty(row[i].OwningColumn.DataPropertyName);
field?.SetValue(element, row[i].Value); 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) public void Fill<TType>(IList<TType> insertValues)
{ {
Clear(); Clear();
@ -93,10 +113,10 @@ namespace Cop.Borovkov.Var3.Components
var row = insertValues[i]; var row = insertValues[i];
outDataGridView.Rows.Add(); 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 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 namespace Cop.Borovkov.Var3.Components
{ {
/// <summary>
/// Визуальный компонент выбора из списка значений
/// </summary>
public partial class CustomListBox : UserControl public partial class CustomListBox : UserControl
{ {
private event EventHandler? _selectedChanged; private event EventHandler? _selectedChanged;

View File

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

View File

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