PIbd-23_Baryshev_D.A._Garage/ProjectGarage/Forms/FormReplenishments.cs

102 lines
3.3 KiB
C#
Raw Normal View History

2024-11-20 14:50:06 +04:00
using ProjectGarage.Repositories;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity;
namespace ProjectGarage.Forms
{
public partial class FormReplenishments : Form
{
private readonly IUnityContainer _container;
private readonly IReplenishmentRepository _replenishmentRepository;
public FormReplenishments(IUnityContainer container, IReplenishmentRepository replenishmentRepository)
{
InitializeComponent();
_container = container ??
throw new ArgumentNullException(nameof(container));
_replenishmentRepository = replenishmentRepository ??
throw new ArgumentNullException(nameof(replenishmentRepository));
}
private void FormReplenishments_Load(object sender, EventArgs e)
{
try
{
LoadList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при загрузке",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public FormReplenishments(IContainer container)
{
container.Add(this);
InitializeComponent();
}
private void ButtonAddReplenishment_Click(object sender, EventArgs e)
{
try
{
_container.Resolve<FormReplenishment>().ShowDialog();
LoadList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при добавлении", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonDeleteReplenishment_Click(object sender, EventArgs e)
{
if (!TryGetIdentifierFromSelectedRow(out var findId))
{
return;
}
if (MessageBox.Show("Удалить запись?", "Удаление", MessageBoxButtons.YesNo) != DialogResult.Yes)
{
return;
}
try
{
_replenishmentRepository.DeleteFuelReplenishment(findId);
LoadList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при удалении",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void LoadList()
{
dataGridViewReplenishments.DataSource = _replenishmentRepository.ReadFuelReplenishment();
dataGridViewReplenishments.Columns["Id"].Visible = false;
dataGridViewReplenishments.Columns["ReplenishmentDate"].DefaultCellStyle.Format = "dd.MM.yyyy";
}
2024-11-20 14:50:06 +04:00
private bool TryGetIdentifierFromSelectedRow(out int id)
{
id = 0;
if (dataGridViewReplenishments.SelectedRows.Count < 1)
{
MessageBox.Show("Нет выбранной записи", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
id =
Convert.ToInt32(dataGridViewReplenishments.SelectedRows[0].Cells["Id"].Value);
return true;
}
}
}