ISEbd-22_Rozhkov.I.E._Simple/GasStation/Forms/FormGasmen.cs

105 lines
3.2 KiB
C#
Raw Normal View History

2024-11-13 23:20:06 +04:00
using GasStation.Repositories;
using Unity;
namespace GasStation.Forms
{
public partial class FormGasmen : Form
{
private readonly IUnityContainer _container;
private readonly IGasmanRepository _gasmanRepository;
public FormGasmen(IUnityContainer container, IGasmanRepository gasmanRepository)
{
InitializeComponent();
_container = container ??
throw new ArgumentNullException(nameof(container));
_gasmanRepository = gasmanRepository ??
throw new ArgumentNullException(nameof(gasmanRepository));
}
private void ButtonAdd_Click(object sender, EventArgs e)
{
try
{
_container.Resolve<FormGasman>().ShowDialog();
LoadList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при добавлении", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonDel_Click(object sender, EventArgs e)
{
if (!TryGetIDFromSelectedRow(out var findID))
{
return;
}
if (MessageBox.Show("Удалить запись?", "Удаление", MessageBoxButtons.YesNo) != DialogResult.Yes)
{
return;
}
try
{
_gasmanRepository.DeleteGasman(findID);
LoadList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка удаления", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonUpd_Click(object sender, EventArgs e)
{
if (!TryGetIDFromSelectedRow(out var findID))
{
return;
}
try
{
var form = _container.Resolve<FormGasman>();
form.ID = findID;
form.ShowDialog();
LoadList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при изменении", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void FormGasmen_Load(object sender, EventArgs e)
{
try
{
LoadList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при загрузке", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void LoadList() => dataGridViewData.DataSource = _gasmanRepository.ReadGasman();
private bool TryGetIDFromSelectedRow(out int id)
{
id = 0;
if (dataGridViewData.SelectedRows.Count < 1)
{
MessageBox.Show("Нет выбранной записи", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
id = Convert.ToInt32(dataGridViewData.SelectedRows[0].Cells["ID"].Value);
return true;
}
}
}