103 lines
2.9 KiB
C#
103 lines
2.9 KiB
C#
using ProjectGSM.Repositories;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using Unity;
|
|
|
|
namespace ProjectGSM.Forms
|
|
{
|
|
public partial class FormCases : Form
|
|
{
|
|
private readonly IUnityContainer _container;
|
|
|
|
private readonly ICaseRepository _caseRepository;
|
|
public FormCases(IUnityContainer container, ICaseRepository caseRepository)
|
|
{
|
|
InitializeComponent();
|
|
_container = container ??
|
|
throw new ArgumentNullException(nameof(container));
|
|
_caseRepository = caseRepository ??
|
|
throw new
|
|
ArgumentNullException(nameof(caseRepository));
|
|
|
|
}
|
|
|
|
private void deleteButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (!TryGetIdentifierFromSelectedRow(out var findId))
|
|
{
|
|
return;
|
|
}
|
|
if (MessageBox.Show("Удалить запись?", "Удаление",
|
|
MessageBoxButtons.YesNo) != DialogResult.Yes)
|
|
{
|
|
return;
|
|
}
|
|
try
|
|
{
|
|
_caseRepository.DeleteCase(findId);
|
|
LoadList();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при удалении",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
|
|
}
|
|
|
|
private void addButton_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
_container.Resolve<FormCase>().ShowDialog();
|
|
LoadList();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при добавлении",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
|
|
}
|
|
|
|
private void FormClients_Load(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
LoadList();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при загрузке",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
|
|
}
|
|
|
|
private void LoadList() => dataGridView1.DataSource =
|
|
_caseRepository.ReadCases();
|
|
|
|
private bool TryGetIdentifierFromSelectedRow(out int id)
|
|
{
|
|
id = 0;
|
|
if (dataGridView1.SelectedRows.Count < 1)
|
|
{
|
|
MessageBox.Show("Нет выбранной записи", "Ошибка",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return false;
|
|
}
|
|
id =
|
|
Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Id"].Value);
|
|
return true;
|
|
}
|
|
|
|
}
|
|
}
|