113 lines
4.6 KiB
C#
113 lines
4.6 KiB
C#
using Npgsql;
|
|
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;
|
|
using System.Windows.Forms.VisualStyles;
|
|
|
|
namespace DeviceAdmin
|
|
{
|
|
public partial class FormKits : Form
|
|
{
|
|
NpgsqlConnection connection = Program.connection;
|
|
private DataTable dataTable;
|
|
private NpgsqlDataAdapter adapter;
|
|
private NpgsqlDataAdapter adapter2;
|
|
private NpgsqlDataAdapter adapter3;
|
|
private DataTable dataTableKit = new();
|
|
private DataTable dataTableCabinet = new();
|
|
public FormKits()
|
|
{
|
|
InitializeComponent();
|
|
LoadGridView();
|
|
adapter3 = new NpgsqlDataAdapter("SELECT id, CONCAT(building, ' ', room) AS cabinet FROM cabinets;", connection);
|
|
adapter3.Fill(dataTableCabinet);
|
|
comboBoxCabinet.DataSource = dataTableCabinet;
|
|
}
|
|
|
|
private void LoadGridView()
|
|
{
|
|
adapter = new NpgsqlDataAdapter("SELECT kits.id, kits.title, cabinets.room, cabinets.building FROM kits LEFT JOIN cabinets ON kits.cabinet_id=cabinets.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 = "Корпус";
|
|
}
|
|
private void ButtonAdd_Click(object sender, EventArgs e)
|
|
{
|
|
if ((comboBoxCabinet.SelectedValue) == null)
|
|
{
|
|
NpgsqlCommand command = new NpgsqlCommand("INSERT INTO kits VALUES (nextval('seq_kits'), @title, default);", connection);
|
|
command.Parameters.AddWithValue("@title", textBoxKit.Text);
|
|
command.ExecuteNonQuery();
|
|
} else
|
|
{
|
|
NpgsqlCommand command = new NpgsqlCommand("INSERT INTO kits VALUES (nextval('seq_kits'), @title, @cabinet_id);", connection);
|
|
command.Parameters.AddWithValue("@title", textBoxKit.Text);
|
|
command.Parameters.AddWithValue("@cabinet_id", (int)comboBoxCabinet.SelectedValue);
|
|
command.ExecuteNonQuery();
|
|
}
|
|
LoadGridView();
|
|
}
|
|
|
|
private void ButtonEdit_Click(object sender, EventArgs e)
|
|
{
|
|
if (dataGridView.SelectedRows.Count > 0)
|
|
{
|
|
if ((comboBoxCabinet.SelectedValue) == null)
|
|
{
|
|
int id = (int)dataGridView.SelectedRows[0].Cells[0].Value;
|
|
NpgsqlCommand command = new NpgsqlCommand("UPDATE kits SET title = @title, cabinet_id = default WHERE Id =@Id;", connection);
|
|
command.Parameters.AddWithValue("@Id", id);
|
|
command.Parameters.AddWithValue("@title", textBoxKit.Text);
|
|
command.ExecuteNonQuery();
|
|
}
|
|
else
|
|
{
|
|
int id = (int)dataGridView.SelectedRows[0].Cells[0].Value;
|
|
NpgsqlCommand command = new NpgsqlCommand("UPDATE kits SET title = @title, cabinet_id = @cabinet_id WHERE Id =@Id;", connection);
|
|
command.Parameters.AddWithValue("@Id", id);
|
|
command.Parameters.AddWithValue("@title", textBoxKit.Text);
|
|
command.Parameters.AddWithValue("@cabinet_id", (int)comboBoxCabinet.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 kits 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();
|
|
}
|
|
}
|
|
}
|