1 lab
This commit is contained in:
parent
f675fa2189
commit
9d28a973d8
16
COP_9/WinFormsLibrary1/ColumnConfiguration.cs
Normal file
16
COP_9/WinFormsLibrary1/ColumnConfiguration.cs
Normal 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; }
|
||||
}
|
||||
}
|
@ -4,14 +4,10 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WinFormsLibrary1
|
||||
{
|
||||
public class ColumnsConfiguratoin
|
||||
namespace WinFormsLibrary1
|
||||
{
|
||||
public int ColumnsCount { get; set; }
|
||||
public string[] NameColumn { get; set; }
|
||||
public int[] Width { get; set; }
|
||||
public bool[] Visible { get; set; }
|
||||
public string[] PropertiesObject { get; set; }
|
||||
public class ColumnsConfiguratoin
|
||||
{
|
||||
public List<ColumnConfiguration> Columns { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,72 +15,59 @@ namespace WinFormsLibrary1
|
||||
public partial class DateBoxWithNull : UserControl
|
||||
{
|
||||
public EventHandler? _changeEvent;
|
||||
public EventHandler? _checkBoxEvent;
|
||||
public Exception? Error;
|
||||
|
||||
public DateBoxWithNull()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
public DateTime? Value
|
||||
{
|
||||
get
|
||||
{
|
||||
Error = null;
|
||||
if (!CheckBoxNull.Checked)
|
||||
{
|
||||
if (CheckBoxNull.Checked)
|
||||
return null;
|
||||
|
||||
if (string.IsNullOrEmpty(TextBoxDate.Text))
|
||||
{
|
||||
Error = new NotFilledException("Text box can't be empty, click checkbox if value must be empty!");
|
||||
return null;
|
||||
throw new NotFilledException("Text box can't be empty, click checkbox if value must be empty!");
|
||||
}
|
||||
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;
|
||||
}
|
||||
else
|
||||
{
|
||||
Error = new ParseException($"Wrong format <{TextBoxDate.Text}>!");
|
||||
return null;
|
||||
throw new ParseException($"Wrong format <{TextBoxDate.Text}>!");
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
if (value is null)
|
||||
CheckBoxNull.Checked = value is null;
|
||||
|
||||
if (value is not null)
|
||||
{
|
||||
CheckBoxNull.Checked = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
TextBoxDate.Text = value?.ToString("dd/MM/yyyy");
|
||||
CheckBoxNull.Checked = false;
|
||||
TextBoxDate.Text = value.Value.ToString("dd.MM.yyyy");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public event EventHandler CheckBoxEvent
|
||||
{
|
||||
add { _checkBoxEvent += value; }
|
||||
remove { _checkBoxEvent += value; }
|
||||
}
|
||||
|
||||
public event EventHandler ChangeEvent
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
private void CheckBoxNull_CheckedChanged(object sender, EventArgs e)
|
||||
{
|
||||
TextBoxDate.Enabled = !CheckBoxNull.Checked;
|
||||
_checkBoxEvent?.Invoke(sender, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -29,17 +29,13 @@ namespace WinFormsLibrary1
|
||||
{
|
||||
get
|
||||
{
|
||||
return (ListBoxCustom.SelectedIndex != -1 && ListBoxCustom.SelectedItem != null)
|
||||
? ListBoxCustom.SelectedItem.ToString()
|
||||
: string.Empty;
|
||||
return ListBoxCustom.SelectedItem?.ToString() ?? string.Empty;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
if (ListBoxCustom.Items.Contains(value))
|
||||
{
|
||||
int index = ListBoxCustom.FindString(value);
|
||||
if (index == -1) return;
|
||||
ListBoxCustom.SetSelected(index, true);
|
||||
ListBoxCustom.SelectedItem = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -49,4 +45,4 @@ namespace WinFormsLibrary1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -23,11 +23,10 @@ namespace WinFormsLibrary1
|
||||
{
|
||||
if (DataGridViewItems.SelectedRows.Count <= value || value < 0)
|
||||
throw new ArgumentException(string.Format("{0} is an invalid row index.", value));
|
||||
else
|
||||
{
|
||||
DataGridViewItems.ClearSelection();
|
||||
DataGridViewItems.Rows[value].Selected = true;
|
||||
}
|
||||
|
||||
DataGridViewItems.ClearSelection();
|
||||
DataGridViewItems.Rows[value].Selected = true;
|
||||
|
||||
}
|
||||
}
|
||||
public void ClearDataGrid()
|
||||
@ -38,13 +37,14 @@ namespace WinFormsLibrary1
|
||||
|
||||
public void ConfigColumn(ColumnsConfiguratoin columnsData)
|
||||
{
|
||||
DataGridViewItems.ColumnCount = columnsData.ColumnsCount;
|
||||
for (int i = 0; i < columnsData.ColumnsCount; i++)
|
||||
DataGridViewItems.ColumnCount = columnsData.Columns.Count;
|
||||
for (int i = 0; i < columnsData.Columns.Count; i++)
|
||||
{
|
||||
DataGridViewItems.Columns[i].Name = columnsData.NameColumn[i];
|
||||
DataGridViewItems.Columns[i].Width = columnsData.Width[i];
|
||||
DataGridViewItems.Columns[i].Visible = columnsData.Visible[i];
|
||||
DataGridViewItems.Columns[i].DataPropertyName = columnsData.PropertiesObject[i];
|
||||
var columnConfig = columnsData.Columns[i];
|
||||
DataGridViewItems.Columns[i].Name = columnConfig.Name;
|
||||
DataGridViewItems.Columns[i].Width = columnConfig.Width;
|
||||
DataGridViewItems.Columns[i].Visible = columnConfig.Visible;
|
||||
DataGridViewItems.Columns[i].DataPropertyName = columnConfig.PropertyName;
|
||||
}
|
||||
}
|
||||
|
||||
@ -53,23 +53,34 @@ namespace WinFormsLibrary1
|
||||
T val = new();
|
||||
|
||||
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;
|
||||
int columnIndex = 0;
|
||||
for (; columnIndex < DataGridViewItems.Columns.Count; columnIndex++)
|
||||
var column = columns.FirstOrDefault(c => c.DataPropertyName == property.Name);
|
||||
|
||||
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;
|
||||
break;
|
||||
propertyType = Nullable.GetUnderlyingType(propertyType);
|
||||
}
|
||||
|
||||
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;
|
||||
|
Loading…
Reference in New Issue
Block a user