using Npgsql; using System.Data; namespace DeviceAdmin { public partial class FormStaff : Form { NpgsqlConnection connection = Program.connection; private DataTable dataTable; private NpgsqlDataAdapter adapter; public FormStaff() { InitializeComponent(); LoadGridView(); } private void LoadGridView() { adapter = new NpgsqlDataAdapter("SELECT * FROM staff;", 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 staff VALUES (nextval('seq_staff'), @surname || ' ' || @name || ' ' ||@patronymic, @department);", connection); command.Parameters.AddWithValue("@surname", textBoxSurname.Text); command.Parameters.AddWithValue("@name", textBoxName.Text); command.Parameters.AddWithValue("@patronymic", textBoxPatronymic.Text); command.Parameters.AddWithValue("@department", textBoxDepartment.Text); 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 staff SET full_name = @surname || ' ' || @name || ' ' ||@patronymic, department = @department WHERE Id = @Id", connection); command.Parameters.AddWithValue("@Id", id); command.Parameters.AddWithValue("@surname", textBoxSurname.Text); command.Parameters.AddWithValue("@name", textBoxName.Text); command.Parameters.AddWithValue("@patronymic", textBoxPatronymic.Text); command.Parameters.AddWithValue("@department", textBoxDepartment.Text); 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 staff 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(); } } }