2024-05-06 22:17:29 +04:00

202 lines
8.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 FormOrder : Form
{
private Abstractions bd;
public FormOrder(Abstractions _bd)
{
InitializeComponent();
bd = _bd;
}
private void loadData()
{
// Получаем список заказов
List<Order> orders = bd.GetOrders();
// Очищаем dataGridView перед заполнением новыми данными
dataGridView.Rows.Clear();
// Предварительно определяем столбцы, если это не было сделано ранее
if (dataGridView.ColumnCount == 0)
{
dataGridView.Columns.Add("Id", "ID");
dataGridView.Columns.Add("ComponentId", "ComponentId");
dataGridView.Columns["ComponentId"].Visible = false;
dataGridView.Columns.Add("Component", "Component");
dataGridView.Columns.Add("ClientId", "ClientId");
dataGridView.Columns["ClientId"].Visible = false;
dataGridView.Columns.Add("Client", "Client");
dataGridView.Columns.Add("ServiceId", "ServiceId");
dataGridView.Columns["ServiceId"].Visible = false;
dataGridView.Columns.Add("Service", "Service");
dataGridView.Columns.Add("DateStart", "Start Date");
dataGridView.Columns.Add("EndDate", "End Date");
dataGridView.Columns.Add("EndCost", "EndCost");
}
// Заполняем dataGridView данными из списка заказов
foreach (Order order in orders)
{
dataGridView.Rows.Add(order.Id,
order.ComponentId,
bd.GetComponentById(order.ComponentId).Title + " " + bd.GetComponentById(order.ComponentId).Manufacturer,
order.ClientId,
bd.GetClientById(order.ClientId).Name + " " + bd.GetClientById(order.ClientId).Surname,
order.ServiceId,
bd.GetServiceById(order.ServiceId).Title,
order.DateStart, order.DateEnd, order.EndCost);
}
}
private void buttonCreate_Click(object sender, EventArgs e)
{
// Создаем новый объект Order и заполняем его данными из текстовых полей и комбо-боксов
Order newOrder = new Order
{
ComponentId = ((Component)comboBoxComponent.SelectedItem).Id,
ClientId = ((Client)comboBoxClient.SelectedItem).Id,
ServiceId = ((Service)comboBoxService.SelectedItem).Id,
EndCost = decimal.Parse(textBoxCost.Text),
DateStart = textBoxStartDate.Text,
DateEnd = textBoxEndDate.Text
};
// Вызываем метод добавления нового заказа в базу данных
bd.AddOrder(newOrder);
// Обновляем dataGridView
loadData();
}
private void buttonUpdate_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
// Получаем индекс выбранной строки
int rowIndex = dataGridView.CurrentCell.RowIndex;
// Получаем ID редактируемого заказа из выбранной строки dataGridView
int orderId = (int)dataGridView.Rows[rowIndex].Cells["Id"].Value;
// Создаем объект Order и заполняем его данными из текстовых полей и комбо-боксов
Order updatedOrder = new Order
{
Id = orderId,
EndCost = decimal.Parse(textBoxCost.Text),
DateStart = textBoxStartDate.Text,
DateEnd = textBoxEndDate.Text,
ComponentId = ((Component)comboBoxComponent.SelectedItem).Id,
ClientId = ((Client)comboBoxClient.SelectedItem).Id,
ServiceId = ((Service)comboBoxService.SelectedItem).Id
};
// Вызываем метод обновления заказа в базе данных
bd.UpdateOrder(updatedOrder);
// Обновляем dataGridView
loadData();
}
}
private void buttonDelete_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
// Получаем индекс выбранной строки
int rowIndex = dataGridView.CurrentCell.RowIndex;
// Получаем ID удаляемого заказа из выбранной строки dataGridView
int orderId = (int)dataGridView.Rows[rowIndex].Cells["Id"].Value;
// Вызываем метод удаления заказа из базы данных
bd.DeleteOrder(orderId);
// Обновляем dataGridView
loadData();
}
}
private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = dataGridView.Rows[e.RowIndex];
// Заполняем текстовые поля данными из выбранной строки
textBoxCost.Text = row.Cells["EndCost"].Value.ToString();
textBoxStartDate.Text = row.Cells["DateStart"].Value.ToString();
textBoxEndDate.Text = row.Cells["EndDate"].Value.ToString();
// Получаем значения для комбо-боксов из выбранной строки
int componentId = Convert.ToInt32(row.Cells["ComponentId"].Value);
int clientId = Convert.ToInt32(row.Cells["ClientId"].Value);
int serviceId = Convert.ToInt32(row.Cells["ServiceId"].Value);
// Заполняем комбо-боксы данными из БД
//comboBoxComponent.DataSource = bd.GetComponents()
// .Select(x => new helpCombobox()
// {
// Text = x.Title + " " + x.Manufacturer,
// Id = x.Id
// })
// .ToList();
//comboBoxComponent.DisplayMember = "Text";
//comboBoxComponent.ValueMember = "Id";
//comboBoxComponent.SelectedValue = componentId;
//comboBoxClient.DataSource = bd.GetClients()
// .Select(x => new helpCombobox()
// {
// Text = x.Name + " " + x.Surname,
// Id = x.Id
// }).ToList();
//comboBoxClient.DisplayMember = "Text";
//comboBoxClient.ValueMember = "Id";
//comboBoxClient.SelectedValue = clientId;
//comboBoxService.DataSource = bd.GetServices()
// .Select(x => new helpCombobox()
// {
// Text = x.Title,
// Id = x.Id
// }).ToList();
//comboBoxService.DisplayMember = "Text";
//comboBoxService.ValueMember = "Id";
//comboBoxService.SelectedValue = serviceId; // Здесь должно быть serviceId, а не clientId
}
}
private void FormOrder_Load(object sender, EventArgs e)
{
// Заполнение comboBoxClientId данными из БД
comboBoxComponent.DataSource = bd.GetComponents();
comboBoxComponent.DisplayMember = "Name";
comboBoxComponent.ValueMember = "Id";
// Заполнение comboBoxOrderId данными из БД
comboBoxClient.DataSource = bd.GetClients();
comboBoxClient.DisplayMember = "Title";
comboBoxClient.ValueMember = "Id";
// Заполнение comboBoxOrderId данными из БД
comboBoxService.DataSource = bd.GetServices();
comboBoxService.DisplayMember = "Title";
comboBoxService.ValueMember = "Id";
loadData();
}
}
}