This commit is contained in:
Казначеева Елизавета 2024-10-01 11:25:36 +04:00
parent f675fa2189
commit 9d28a973d8
5 changed files with 79 additions and 73 deletions

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WinFormsLibrary1
{
public class ColumnConfiguration
{
public string Name { get; set; }
public int Width { get; set; }
public bool Visible { get; set; }
public string PropertyName { get; set; }
}
}

View File

@ -4,14 +4,10 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace WinFormsLibrary1 namespace WinFormsLibrary1
{
public class ColumnsConfiguratoin
{ {
public int ColumnsCount { get; set; } public class ColumnsConfiguratoin
public string[] NameColumn { get; set; } {
public int[] Width { get; set; } public List<ColumnConfiguration> Columns { get; set; }
public bool[] Visible { get; set; } }
public string[] PropertiesObject { get; set; }
} }
}

View File

@ -15,72 +15,59 @@ namespace WinFormsLibrary1
public partial class DateBoxWithNull : UserControl public partial class DateBoxWithNull : UserControl
{ {
public EventHandler? _changeEvent; public EventHandler? _changeEvent;
public EventHandler? _checkBoxEvent;
public Exception? Error; public Exception? Error;
public DateBoxWithNull() public DateBoxWithNull()
{ {
InitializeComponent(); InitializeComponent();
} }
public DateTime? Value public DateTime? Value
{ {
get get
{ {
Error = null; if (CheckBoxNull.Checked)
if (!CheckBoxNull.Checked) return null;
{
if (string.IsNullOrEmpty(TextBoxDate.Text)) if (string.IsNullOrEmpty(TextBoxDate.Text))
{ {
Error = new NotFilledException("Text box can't be empty, click checkbox if value must be empty!"); throw new NotFilledException("Text box can't be empty, click checkbox if value must be empty!");
return null;
} }
if (DateTime.TryParseExact(TextBoxDate.Text, "dd/MM/yyyy", null, DateTimeStyles.None, out DateTime parsedDate)) if (DateTime.TryParseExact(TextBoxDate.Text, "dd.MM.yyyy", null, System.Globalization.DateTimeStyles.None, out DateTime parsedDate))
{ {
return parsedDate; return parsedDate;
} }
else else
{ {
Error = new ParseException($"Wrong format <{TextBoxDate.Text}>!"); throw new ParseException($"Wrong format <{TextBoxDate.Text}>!");
return null;
} }
}
return null;
} }
set set
{ {
if (value is null) CheckBoxNull.Checked = value is null;
if (value is not null)
{ {
CheckBoxNull.Checked = true; TextBoxDate.Text = value.Value.ToString("dd.MM.yyyy");
}
else
{
TextBoxDate.Text = value?.ToString("dd/MM/yyyy");
CheckBoxNull.Checked = false;
} }
} }
} }
public event EventHandler CheckBoxEvent
{
add { _checkBoxEvent += value; }
remove { _checkBoxEvent += value; }
}
public event EventHandler ChangeEvent public event EventHandler ChangeEvent
{ {
add { _changeEvent += value; } add { _changeEvent += value; }
remove { _changeEvent += value; } remove { _changeEvent -= value; }
} }
private void TextBoxDate_TextChanged(object sender, EventArgs e) private void OnControlChanged(object sender, EventArgs e)
{ {
if (sender is CheckBox)
{
TextBoxDate.Enabled = !CheckBoxNull.Checked;
}
_changeEvent?.Invoke(sender, e); _changeEvent?.Invoke(sender, e);
} }
private void CheckBoxNull_CheckedChanged(object sender, EventArgs e)
{
TextBoxDate.Enabled = !CheckBoxNull.Checked;
_checkBoxEvent?.Invoke(sender, e);
}
} }
} }

View File

@ -29,17 +29,13 @@ namespace WinFormsLibrary1
{ {
get get
{ {
return (ListBoxCustom.SelectedIndex != -1 && ListBoxCustom.SelectedItem != null) return ListBoxCustom.SelectedItem?.ToString() ?? string.Empty;
? ListBoxCustom.SelectedItem.ToString()
: string.Empty;
} }
set set
{ {
if (!string.IsNullOrEmpty(value)) if (ListBoxCustom.Items.Contains(value))
{ {
int index = ListBoxCustom.FindString(value); ListBoxCustom.SelectedItem = value;
if (index == -1) return;
ListBoxCustom.SetSelected(index, true);
} }
} }
} }
@ -49,4 +45,4 @@ namespace WinFormsLibrary1
} }
} }
} }

View File

@ -23,11 +23,10 @@ namespace WinFormsLibrary1
{ {
if (DataGridViewItems.SelectedRows.Count <= value || value < 0) if (DataGridViewItems.SelectedRows.Count <= value || value < 0)
throw new ArgumentException(string.Format("{0} is an invalid row index.", value)); throw new ArgumentException(string.Format("{0} is an invalid row index.", value));
else
{ DataGridViewItems.ClearSelection();
DataGridViewItems.ClearSelection(); DataGridViewItems.Rows[value].Selected = true;
DataGridViewItems.Rows[value].Selected = true;
}
} }
} }
public void ClearDataGrid() public void ClearDataGrid()
@ -38,13 +37,14 @@ namespace WinFormsLibrary1
public void ConfigColumn(ColumnsConfiguratoin columnsData) public void ConfigColumn(ColumnsConfiguratoin columnsData)
{ {
DataGridViewItems.ColumnCount = columnsData.ColumnsCount; DataGridViewItems.ColumnCount = columnsData.Columns.Count;
for (int i = 0; i < columnsData.ColumnsCount; i++) for (int i = 0; i < columnsData.Columns.Count; i++)
{ {
DataGridViewItems.Columns[i].Name = columnsData.NameColumn[i]; var columnConfig = columnsData.Columns[i];
DataGridViewItems.Columns[i].Width = columnsData.Width[i]; DataGridViewItems.Columns[i].Name = columnConfig.Name;
DataGridViewItems.Columns[i].Visible = columnsData.Visible[i]; DataGridViewItems.Columns[i].Width = columnConfig.Width;
DataGridViewItems.Columns[i].DataPropertyName = columnsData.PropertiesObject[i]; DataGridViewItems.Columns[i].Visible = columnConfig.Visible;
DataGridViewItems.Columns[i].DataPropertyName = columnConfig.PropertyName;
} }
} }
@ -53,23 +53,34 @@ namespace WinFormsLibrary1
T val = new(); T val = new();
var propertiesObj = typeof(T).GetProperties(); var propertiesObj = typeof(T).GetProperties();
foreach (var properties in propertiesObj)
var columns = DataGridViewItems.Columns.Cast<DataGridViewColumn>().ToList();
foreach (var property in propertiesObj)
{ {
bool propIsExist = false; var column = columns.FirstOrDefault(c => c.DataPropertyName == property.Name);
int columnIndex = 0;
for (; columnIndex < DataGridViewItems.Columns.Count; columnIndex++) if (column != null)
{ {
if (DataGridViewItems.Columns[columnIndex].DataPropertyName.ToString() == properties.Name) object value = DataGridViewItems.SelectedRows[0].Cells[column.Index].Value;
Type propertyType = property.PropertyType;
bool isNullable = Nullable.GetUnderlyingType(propertyType) != null;
if (isNullable)
{ {
propIsExist = true; propertyType = Nullable.GetUnderlyingType(propertyType);
break; }
if (value != DBNull.Value)
{
property.SetValue(val, Convert.ChangeType(value, propertyType));
}
else if (isNullable)
{
property.SetValue(val, null);
} }
} }
if (propIsExist)
{
object value = DataGridViewItems.SelectedRows[0].Cells[columnIndex].Value;
properties.SetValue(val, Convert.ChangeType(value, properties?.PropertyType));
};
} }
return val; return val;