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.Data.SqlClient; namespace proba { public partial class Genre : Form { string id_genre; string genre; bool save; DataBase dataBase = new DataBase(); public Genre() { InitializeComponent(); } private void CreateColumns() { dataGridView1.Columns.Add("genre", "Жанр музыки"); dataGridView1.Columns.Add("", String.Empty); } private void ReadSingleRow(DataGridView dgw, IDataRecord record) { dgw.Rows.Add(record.GetString(0), RowState.ModifiedNew); } private void RefresDataGird(DataGridView dgw) { dgw.Rows.Clear(); dataGridView1.Columns[1].Visible = false; string queryString = $"Select genre from Genre"; SqlCommand command = new SqlCommand(queryString, dataBase.GetConnection()); dataBase.openConnection(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { ReadSingleRow(dgw, reader); } reader.Close(); } private void button_add_Click(object sender, EventArgs e) { this.Hide(); Genre_add genre_Add = new Genre_add(); genre_Add.ShowDialog(); } private void button_back_Click(object sender, EventArgs e) { this.Hide(); MainMenu mainMenu = new MainMenu(); mainMenu.ShowDialog(); } public void global_FormClosed(object sender, EventArgs e) { Application.Exit(); } private void deleteRow() { int index = dataGridView1.CurrentCell.RowIndex; dataGridView1.Rows[index].Visible = false; if (dataGridView1.Rows[index].Cells[0].Value.ToString() == String.Empty) { dataGridView1.Rows[index].Cells[1].Value = RowState.Deleted; return; } dataGridView1.Rows[index].Cells[1].Value = RowState.Deleted; id_genre = null; } new private void Update() { dataBase.openConnection(); for (int index = 0; index < dataGridView1.Rows.Count; index++) { var rowState = (RowState)dataGridView1.Rows[index].Cells[1].Value; if (rowState == RowState.Existed) continue; if (rowState == RowState.Deleted) { SqlCommand sqlCommand_genre = new SqlCommand($"SELECT id_genre From Genre WHERE genre = '{dataGridView1.Rows[index].Cells[0].Value}'", dataBase.GetConnection()); var id = sqlCommand_genre.ExecuteScalar().ToString(); var deleteQuery = $"delete from Genre where id_genre = {id}"; var command = new SqlCommand(deleteQuery, dataBase.GetConnection()); command.ExecuteNonQuery(); } } dataBase.closeConnection(); } private void StrokaSearch_TextChanged(object sender, EventArgs e) { Search(dataGridView1); } private void Search(DataGridView dgw) { dgw.Rows.Clear(); string searchString = $"Select genre from Genre Where genre like '%" + StrokaSearch.Text + "%'"; SqlCommand com = new SqlCommand(searchString, dataBase.GetConnection()); dataBase.openConnection(); SqlDataReader read = com.ExecuteReader(); while (read.Read()) { ReadSingleRow(dgw, read); } read.Close(); } private void pictureBox1_Click(object sender, EventArgs e) { RefresDataGird(dataGridView1); save = false; } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { dataBase.openConnection(); var selectedRow = e.RowIndex; if (e.RowIndex >= 0) { DataGridViewRow row = dataGridView1.Rows[selectedRow]; genre = row.Cells[0].Value.ToString(); SqlCommand sqlCommand_genre = new SqlCommand($"SELECT id_genre From Genre WHERE genre = '{genre}'", dataBase.GetConnection()); id_genre = sqlCommand_genre.ExecuteScalar().ToString(); } dataBase.closeConnection(); } private void button_insert_Click(object sender, EventArgs e) { if (id_genre == null) { MessageBox.Show("Не выделена строчка для изменения"); } else { this.Hide(); Genre_update genre_Update = new Genre_update(id_genre, genre); genre_Update.ShowDialog(); } } private void Genre_Load(object sender, EventArgs e) { CreateColumns(); RefresDataGird(dataGridView1); } private void button_delete_Click_1(object sender, EventArgs e) { dataBase.openConnection(); string admin = $"SELECT P.id_product From Genre G inner join Products P ON G.id_genre = P.id_genre WHERE G.genre = '{genre}'"; SqlDataAdapter sda = new SqlDataAdapter(admin, dataBase.GetConnection()); DataTable dtbl = new DataTable(); sda.Fill(dtbl); if (dtbl.Rows.Count < 1) { if (id_genre == null) { MessageBox.Show("Не выделена запись для удаления!!!"); } else { deleteRow(); save = true; } } else { MessageBox.Show("Нельзя удалить запись, на которую есть ссылка!!!"); } dataBase.closeConnection(); } private void button_Save_Click(object sender, EventArgs e) { if (save == true) { Update(); save = false; } } } }