87 lines
4.0 KiB
C#
87 lines
4.0 KiB
C#
using Npgsql;
|
|
using System.Data;
|
|
namespace DeviceAdmin
|
|
{
|
|
public partial class FormOwnership : Form
|
|
{
|
|
NpgsqlConnection connection = Program.connection;
|
|
private DataTable dataTable;
|
|
private NpgsqlDataAdapter adapter;
|
|
private NpgsqlDataAdapter adapter2;
|
|
private NpgsqlDataAdapter adapter3;
|
|
private DataTable dataTableStaff = new();
|
|
private DataTable dataTableKit = new();
|
|
public FormOwnership()
|
|
{
|
|
InitializeComponent();
|
|
LoadGridView();
|
|
adapter2 = new NpgsqlDataAdapter("SELECT id, CONCAT(id, ' ', title) AS title FROM kits", connection);
|
|
adapter2.Fill(dataTableKit);
|
|
comboBoxKit.DataSource = dataTableKit;
|
|
adapter3 = new NpgsqlDataAdapter("SELECT * FROM staff", connection);
|
|
adapter3.Fill(dataTableStaff);
|
|
comboBoxStaff.DataSource = dataTableStaff;
|
|
}
|
|
private void LoadGridView()
|
|
{
|
|
adapter = new NpgsqlDataAdapter("SELECT kit_id, kits.title, staff_id, staff.full_name FROM ownership LEFT JOIN kits ON kit_id=kits.id FULL OUTER JOIN staff ON staff_id=staff.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 = "ID сотрудника";
|
|
columns[3].HeaderText = "ФИО сотрудника";
|
|
}
|
|
private void ButtonAdd_Click(object sender, EventArgs e)
|
|
{
|
|
NpgsqlCommand command = new NpgsqlCommand("INSERT INTO ownership VALUES (@kit, @staff);", connection);
|
|
command.Parameters.AddWithValue("@kit", (int)comboBoxKit.SelectedValue);
|
|
command.Parameters.AddWithValue("@staff", (int)comboBoxStaff.SelectedValue);
|
|
command.ExecuteNonQuery();
|
|
LoadGridView();
|
|
}
|
|
private void ButtonEdit_Click(object sender, EventArgs e)
|
|
{
|
|
if (dataGridView.SelectedRows.Count > 0)
|
|
{
|
|
int kit_id = (int)dataGridView.SelectedRows[0].Cells[0].Value;
|
|
int staff_Id = (int)dataGridView.SelectedRows[0].Cells[2].Value;
|
|
NpgsqlCommand command = new NpgsqlCommand("UPDATE ownership SET kit_id = @kit, staff_id = @staff WHERE kit_id =@kit_id AND staff_id=@staff_id", connection);
|
|
command.Parameters.AddWithValue("@kit_id", kit_id);
|
|
command.Parameters.AddWithValue("@staff_Id", staff_Id);
|
|
command.Parameters.AddWithValue("@kit", (int)comboBoxKit.SelectedValue);
|
|
command.Parameters.AddWithValue("@staff", (int)comboBoxStaff.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 kit_id = (int)dataGridView.SelectedRows[0].Cells[0].Value;
|
|
int staff_Id = (int)dataGridView.SelectedRows[0].Cells[2].Value;
|
|
NpgsqlCommand command = new NpgsqlCommand("DELETE FROM ownership WHERE kit_id = @kit_id and staff_id=@staff_id", connection);
|
|
command.Parameters.AddWithValue("@kit_id", kit_id);
|
|
command.Parameters.AddWithValue("@staff_Id", staff_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();
|
|
}
|
|
}
|
|
}
|