84 lines
4.0 KiB
C#
84 lines
4.0 KiB
C#
using Npgsql;
|
|
using System.Data;
|
|
namespace DeviceAdmin
|
|
{
|
|
public partial class FormServices : Form
|
|
{
|
|
NpgsqlConnection connection = Program.connection;
|
|
private DataTable dataTable;
|
|
private NpgsqlDataAdapter adapter;
|
|
private DataTable dataTableDevice = new();
|
|
private NpgsqlDataAdapter adapter2;
|
|
public FormServices()
|
|
{
|
|
InitializeComponent();
|
|
LoadGridView();
|
|
adapter2 = new NpgsqlDataAdapter("SELECT id, CONCAT(id, ' ', model) as device FROM devices", connection);
|
|
adapter2.Fill(dataTableDevice);
|
|
comboBoxDevice.DataSource = dataTableDevice;
|
|
}
|
|
private void LoadGridView()
|
|
{
|
|
adapter = new NpgsqlDataAdapter("SELECT services.id, services.description, services.start_date, services.end_date, services.device_id, devices.model FROM services JOIN devices ON devices.id=services.device_id;", connection);
|
|
dataTable = new DataTable();
|
|
adapter.Fill(dataTable);
|
|
dataGridView.DataSource = dataTable;
|
|
DataGridViewColumnCollection columns = dataGridView.Columns;
|
|
columns[0].HeaderText = "ID";
|
|
columns[1].HeaderText = "Описание";
|
|
columns[2].HeaderText = "Дата начала";
|
|
columns[3].HeaderText = "Дата конца";
|
|
columns[4].HeaderText = "ID устр.";
|
|
columns[4].HeaderText = "Модель устр.";
|
|
}
|
|
private void ButtonAdd_Click(object sender, EventArgs e)
|
|
{
|
|
NpgsqlCommand command = new NpgsqlCommand("INSERT INTO services VALUES (nextval('seq_services'), @description, @start_date, @end_date, @device_id);", connection);
|
|
command.Parameters.AddWithValue("@description", richTextBoxDescription.Text);
|
|
command.Parameters.AddWithValue("@start_date", dateTimePickerStartDate.Value.Date);
|
|
command.Parameters.AddWithValue("@end_date", dateTimePickerEndDate.Value.Date);
|
|
command.Parameters.AddWithValue("@device_id", (int)comboBoxDevice.SelectedValue);
|
|
command.ExecuteNonQuery();
|
|
LoadGridView();
|
|
}
|
|
private void ButtonEdit_Click(object sender, EventArgs e)
|
|
{
|
|
if (dataGridView.SelectedRows.Count > 0)
|
|
{
|
|
int id = (int)dataGridView.SelectedRows[0].Cells[0].Value;
|
|
NpgsqlCommand command = new NpgsqlCommand("UPDATE services SET description = @description, start_date = @start_date, end_date=@end_date, device_id=@device_id WHERE Id = @Id", connection);
|
|
command.Parameters.AddWithValue("@Id", id);
|
|
command.Parameters.AddWithValue("@description", richTextBoxDescription.Text);
|
|
command.Parameters.AddWithValue("@start_date", dateTimePickerStartDate.Value.Date);
|
|
command.Parameters.AddWithValue("@end_date", dateTimePickerEndDate.Value.Date);
|
|
command.Parameters.AddWithValue("@device_id", (int)comboBoxDevice.SelectedValue);
|
|
command.ExecuteNonQuery();
|
|
LoadGridView();
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Please select a row to edit.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
private void ButtonDelete_Click(object sender, EventArgs e)
|
|
{
|
|
if (dataGridView.SelectedRows.Count > 0)
|
|
{
|
|
int id = (int)dataGridView.SelectedRows[0].Cells[0].Value;
|
|
NpgsqlCommand command = new NpgsqlCommand("DELETE FROM services WHERE Id = @Id", connection);
|
|
command.Parameters.AddWithValue("@Id", id);
|
|
command.ExecuteNonQuery();
|
|
LoadGridView();
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Please select a row to delete.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
private void ButtonRefresh_Click(object sender, EventArgs e)
|
|
{
|
|
LoadGridView();
|
|
}
|
|
}
|
|
}
|