COP_Petrushin_PIbd-32/Laba3/ManufacturerForm.cs

96 lines
3.2 KiB
C#
Raw Permalink Normal View History

2024-10-30 13:34:23 +04:00
using Data.Models;
using Data.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;
namespace View
{
public partial class ManufacturerForm : Form
{
private readonly IManufacturerRepository _manufacturerRepository;
public ManufacturerForm(IManufacturerRepository manufacturerRepository)
{
InitializeComponent();
_manufacturerRepository = manufacturerRepository;
LoadManufacturers();
}
private void LoadManufacturers()
{
var manufacturers = _manufacturerRepository.GetAllManufacturers().ToList();
dataGridView.DataSource = manufacturers;
}
private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
var manufacturer = dataGridView.Rows[e.RowIndex].DataBoundItem as Manufacturer;
if (manufacturer != null)
{
_manufacturerRepository.UpdateManufacturer(manufacturer);
}
}
}
private void dataGridView_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
var manufacturer = e.Row.DataBoundItem as Manufacturer;
if (manufacturer != null)
{
if (MessageBox.Show("Are you sure you want to delete this manufacturer?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
_manufacturerRepository.DeleteManufacturer(manufacturer.Id);
}
else
{
e.Cancel = true;
}
}
}
private void dataGridView_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
var manufacturer = new Manufacturer { Name = "" };
_manufacturerRepository.AddManufacturer(manufacturer);
LoadManufacturers();
}
2024-10-30 18:54:45 +04:00
// реализация через нажатие клавишь
2024-10-30 18:54:45 +04:00
private void dataGridView_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Insert)
{
var manufacturer = new Manufacturer { Name = "" };
_manufacturerRepository.AddManufacturer(manufacturer);
LoadManufacturers();
}
else if (e.KeyCode == Keys.Delete)
{
if (dataGridView.SelectedRows.Count > 0)
{
var selectedManufacturer = dataGridView.SelectedRows[0].DataBoundItem as Manufacturer;
if (selectedManufacturer != null)
{
if (MessageBox.Show("Are you sure you want to delete this manufacturer?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
_manufacturerRepository.DeleteManufacturer(selectedManufacturer.Id);
LoadManufacturers();
}
}
}
}
2024-10-30 18:54:45 +04:00
}
2024-10-30 13:34:23 +04:00
}
}