using Npgsql; using System.Data; namespace DeviceAdmin { public partial class FormKinds : Form { NpgsqlConnection connection = Program.connection; private DataTable dataTable; private NpgsqlDataAdapter adapter; public FormKinds() { InitializeComponent(); LoadGridView(); } private void LoadGridView() { adapter = new NpgsqlDataAdapter("SELECT * FROM kinds;", 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 kinds VALUES (nextval('seq_kinds'), @title, @frequency);", connection); command.Parameters.AddWithValue("@title", textBoxTitle.Text); if (int.Parse(numericUpDownFrequency.Value.ToString()) != 0) { command.Parameters.AddWithValue("@frequency", int.Parse(numericUpDownFrequency.Value.ToString())); } else { command.Parameters.AddWithValue("@frequency", 12); } 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 kinds SET title = @title, frequency = @frequency WHERE Id = @Id", connection); command.Parameters.AddWithValue("@Id", id); command.Parameters.AddWithValue("@title", textBoxTitle.Text); if (int.Parse(numericUpDownFrequency.Value.ToString()) != 0) { command.Parameters.AddWithValue("@frequency", int.Parse(numericUpDownFrequency.Value.ToString())); } else { command.Parameters.AddWithValue("@frequency", 12); } 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 kinds 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(); } } }