141 lines
5.0 KiB
C#
Raw Permalink Normal View History

2024-11-27 23:43:53 +04:00
using BusinessLogic;
using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
using Contracts.SearchModels;
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;
namespace COP3_
{
public partial class FormGuide : Form
{
private readonly ICityStatusLogic _orderStatusLogic;
public FormGuide(ICityStatusLogic orderStatusLogic)
{
_orderStatusLogic = orderStatusLogic;
InitializeComponent();
LoadData();
}
private void LoadData()
{
try
{
var orderStatuses = _orderStatusLogic.ReadList(null);
if (orderStatuses != null)
{
dataGridViewGuide.Rows.Clear();
foreach (var status in orderStatuses)
{
dataGridViewGuide.Rows.Add(status.Name, status.Id);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void dataGridViewGuide_KeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.KeyCode == Keys.Insert)
{
dataGridViewGuide.Rows.Add();
dataGridViewGuide.CurrentCell = dataGridViewGuide.Rows[dataGridViewGuide.Rows.Count - 1].Cells["OrderStatus"]; // Ставим фокус на новую строку
e.Handled = true;
}
if (e.KeyCode == Keys.Delete)
{
if (dataGridViewGuide.SelectedRows.Count == 1)
{
DataGridViewRow currentRow = dataGridViewGuide.SelectedRows[0];
if (currentRow.Cells["Id"].Value != null || Convert.ToInt32(currentRow.Cells["Id"].Value) != 0)
{
var result = MessageBox.Show(
"Подтвердите удаление",
"Подтверждение",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question
);
if (result == DialogResult.Yes)
{
_orderStatusLogic.Delete(new CityBindingModel { Id = Convert.ToInt32(currentRow.Cells["Id"].Value) });
MessageBox.Show("Удалено");
LoadData();
}
else
{
MessageBox.Show("Удаление отменено", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("Нечего удалять", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void dataGridViewGuide_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
string? userInput = e.FormattedValue.ToString();
if (string.IsNullOrWhiteSpace(userInput))
{
MessageBox.Show("Введите значение в поле", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
e.Cancel = true;
}
}
private void dataGridViewGuide_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
try
{
DataGridViewRow currentRow = dataGridViewGuide.Rows[e.RowIndex];
if (currentRow.Cells["Id"].Value == null || Convert.ToInt32(currentRow.Cells["Id"].Value) == 0)
{
_orderStatusLogic.Create(new CityBindingModel
{
Name = currentRow.Cells["OrderStatus"].Value.ToString()!,
});
}
else
{
_orderStatusLogic.Update(new CityBindingModel
{
Id = Convert.ToInt32(currentRow.Cells["Id"].Value),
Name = currentRow.Cells["OrderStatus"].Value.ToString(),
});
}
MessageBox.Show("Новая запись сохранена");
}
catch (Exception ex)
{
MessageBox.Show("Попробуйте ещё раз" + ex.Message);
}
}
}
}