using Npgsql; using System.Data; namespace DeviceAdmin { public partial class FormCabinets : Form { NpgsqlConnection connection = Program.connection; private DataTable dataTable; private NpgsqlDataAdapter adapter; public FormCabinets() { InitializeComponent(); LoadGridView(); } private void LoadGridView() { adapter = new NpgsqlDataAdapter("SELECT * FROM cabinets;", connection); dataTable = new DataTable(); adapter.Fill(dataTable); dataGridView.DataSource = dataTable; DataGridViewColumnCollection columns = dataGridView.Columns; columns[0].HeaderText = "ID"; columns[1].HeaderText = "Кабинет"; columns[2].HeaderText = "Корпус"; } private void ButtonAdd_Click(object sender, EventArgs e) { NpgsqlCommand command = new NpgsqlCommand("INSERT INTO cabinets VALUES (nextval('seq_cabinets'), @Room, @Building);", connection); command.Parameters.AddWithValue("@Room", textBoxRoom.Text); command.Parameters.AddWithValue("@Building", int.Parse(numericUpDownBuilding.Value.ToString())); 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 Cabinets SET Room = @Room, Building = @Building WHERE Id = @Id", connection); command.Parameters.AddWithValue("@Id", id); command.Parameters.AddWithValue("@Room", textBoxRoom.Text); command.Parameters.AddWithValue("@Building", int.Parse(numericUpDownBuilding.Value.ToString())); 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 Cabinets 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(); } } }