134 lines
5.1 KiB
C#
Raw Permalink Normal View History

2024-05-06 22:17:29 +04:00
using database;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Forms
{
public partial class FormComponent : Form
{
private Abstractions bd;
public FormComponent(Abstractions _bd)
{
InitializeComponent();
bd = _bd;
}
private void loadData()
{
// Получаем список компонентов
List<Component> components = bd.GetComponents();
// Очищаем dataGridView перед заполнением новыми данными
dataGridView.Rows.Clear();
// Предварительно определяем столбцы, если это не было сделано ранее
if (dataGridView.ColumnCount == 0)
{
dataGridView.Columns.Add("Id", "ID");
dataGridView.Columns.Add("Title", "Title");
dataGridView.Columns.Add("CountSklad", "CountSklad");
dataGridView.Columns.Add("Price", "Price");
dataGridView.Columns.Add("Manufacturer", "Manufacturer");
}
// Заполняем dataGridView данными из списка компонентов
foreach (Component component in components)
{
dataGridView.Rows.Add(component.Id, component.Title, component.CountSklad, component.Price, component.Manufacturer);
}
}
private void buttonCreate_Click(object sender, EventArgs e)
{
// Создаем новый объект Component и заполняем его данными из текстовых полей
Component newComponent = new Component
{
Title = textBoxTitle.Text,
CountSklad = textBoxCountSklad.Text,
Price = double.Parse(textBoxPrice.Text),
Manufacturer = textBoxManufacturer.Text
};
// Вызываем метод добавления нового компонента в базу данных
bd.AddComponent(newComponent);
// Перезагружаем данные в dataGridView
loadData();
}
private void buttonUpdate_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
// Получаем выбранный компонент из dataGridView
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
int componentId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
// Создаем объект Component с обновленными данными из текстовых полей
Component updatedComponent = new Component
{
Id = componentId,
Title = textBoxTitle.Text,
CountSklad = textBoxCountSklad.Text,
Price = double.Parse(textBoxPrice.Text),
Manufacturer = textBoxManufacturer.Text
};
// Вызываем метод обновления компонента в базе данных
bd.UpdateComponent(updatedComponent);
// Перезагружаем данные в dataGridView
loadData();
}
else
{
MessageBox.Show("Please select a branch to update.");
}
}
private void buttonDelete_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
// Получаем выбранный компонент из dataGridView
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
int componentId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
// Вызываем метод удаления компонентa из базы данных
bd.DeleteComponent(componentId);
// Перезагружаем данные в dataGridView
loadData();
}
else
{
MessageBox.Show("Please select a branch to delete.");
}
}
private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = dataGridView.Rows[e.RowIndex];
textBoxTitle.Text = row.Cells["Title"].Value.ToString();
textBoxCountSklad.Text = row.Cells["CountSklad"].Value.ToString();
textBoxPrice.Text = row.Cells["Price"].Value.ToString();
textBoxManufacturer.Text = row.Cells["Manufacturer"].Value.ToString();
}
}
private void FormComponent_Load(object sender, EventArgs e)
{
loadData();
}
}
}