PIbd-32_Turner_I.A._COP_10/COP/WinForms/FormStatus.cs
2023-10-25 23:06:54 +04:00

110 lines
2.9 KiB
C#

using ClientsContracts.BindingModels;
using ClientsContracts.BusinessLogicContracts;
using System.ComponentModel;
namespace WinForms
{
public partial class FormStatus : Form
{
private readonly IStatusLogic statusLogic;
BindingList<StatusBindingModel> list;
public FormStatus(IStatusLogic _statusLogic)
{
InitializeComponent();
statusLogic = _statusLogic;
list = new BindingList<StatusBindingModel>();
dataGridView.AllowUserToAddRows = false;
}
private void LoadData()
{
try
{
var list1 = statusLogic.Read(null);
list.Clear();
foreach (var item in list1)
{
list.Add(new StatusBindingModel
{
Id = item.Id,
Name = item.Name,
});
}
if (list != null)
{
dataGridView.DataSource = list;
dataGridView.Columns[0].Visible = false;
dataGridView.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void FormStatus_Load(object sender, EventArgs e)
{
LoadData();
}
private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
var typeName = (string)dataGridView.CurrentRow.Cells[1].Value;
if (!string.IsNullOrEmpty(typeName))
{
if (dataGridView.CurrentRow.Cells[0].Value != null)
{
statusLogic.CreateOrUpdate(new StatusBindingModel()
{
Id = Convert.ToInt32(dataGridView.CurrentRow.Cells[0].Value),
Name = (string)dataGridView.CurrentRow.Cells[1].EditedFormattedValue
});
}
else
{
statusLogic.CreateOrUpdate(new StatusBindingModel()
{
Name = (string)dataGridView.CurrentRow.Cells[1].EditedFormattedValue
});
}
}
else
{
MessageBox.Show("Введена пустая строка", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
LoadData();
}
private void dataGridView_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Insert)
{
if (dataGridView.Rows.Count == 0)
{
list.Add(new StatusBindingModel());
dataGridView.DataSource = new BindingList<StatusBindingModel>(list);
dataGridView.CurrentCell = dataGridView.Rows[0].Cells[1];
return;
}
if (dataGridView.Rows[dataGridView.Rows.Count - 1].Cells[1].Value != null)
{
list.Add(new StatusBindingModel());
dataGridView.DataSource = new BindingList<StatusBindingModel>(list);
dataGridView.CurrentCell = dataGridView.Rows[dataGridView.Rows.Count - 1].Cells[1];
return;
}
}
if (e.KeyData == Keys.Delete)
{
if (MessageBox.Show("Удалить выбранный элемент", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
statusLogic.Delete(new StatusBindingModel() { Id = (int)dataGridView.CurrentRow.Cells[0].Value });
LoadData();
}
}
}
}
}