Genre.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data.SqlClient;
  11. namespace proba
  12. {
  13. public partial class Genre : Form
  14. {
  15. string id_genre;
  16. string genre;
  17. bool save;
  18. DataBase dataBase = new DataBase();
  19. public Genre()
  20. {
  21. InitializeComponent();
  22. }
  23. private void CreateColumns()
  24. {
  25. dataGridView1.Columns.Add("genre", "Жанр музыки");
  26. dataGridView1.Columns.Add("", String.Empty);
  27. }
  28. private void ReadSingleRow(DataGridView dgw, IDataRecord record)
  29. {
  30. dgw.Rows.Add(record.GetString(0), RowState.ModifiedNew);
  31. }
  32. private void RefresDataGird(DataGridView dgw)
  33. {
  34. dgw.Rows.Clear();
  35. dataGridView1.Columns[1].Visible = false;
  36. string queryString = $"Select genre from Genre";
  37. SqlCommand command = new SqlCommand(queryString, dataBase.GetConnection());
  38. dataBase.openConnection();
  39. SqlDataReader reader = command.ExecuteReader();
  40. while (reader.Read())
  41. {
  42. ReadSingleRow(dgw, reader);
  43. }
  44. reader.Close();
  45. }
  46. private void button_add_Click(object sender, EventArgs e)
  47. {
  48. this.Hide();
  49. Genre_add genre_Add = new Genre_add();
  50. genre_Add.ShowDialog();
  51. }
  52. private void button_back_Click(object sender, EventArgs e)
  53. {
  54. this.Hide();
  55. MainMenu mainMenu = new MainMenu();
  56. mainMenu.ShowDialog();
  57. }
  58. public void global_FormClosed(object sender, EventArgs e)
  59. {
  60. Application.Exit();
  61. }
  62. private void deleteRow()
  63. {
  64. int index = dataGridView1.CurrentCell.RowIndex;
  65. dataGridView1.Rows[index].Visible = false;
  66. if (dataGridView1.Rows[index].Cells[0].Value.ToString() == String.Empty)
  67. {
  68. dataGridView1.Rows[index].Cells[1].Value = RowState.Deleted;
  69. return;
  70. }
  71. dataGridView1.Rows[index].Cells[1].Value = RowState.Deleted;
  72. id_genre = null;
  73. }
  74. new private void Update()
  75. {
  76. dataBase.openConnection();
  77. for (int index = 0; index < dataGridView1.Rows.Count; index++)
  78. {
  79. var rowState = (RowState)dataGridView1.Rows[index].Cells[1].Value;
  80. if (rowState == RowState.Existed)
  81. continue;
  82. if (rowState == RowState.Deleted)
  83. {
  84. SqlCommand sqlCommand_genre = new SqlCommand($"SELECT id_genre From Genre WHERE genre = '{dataGridView1.Rows[index].Cells[0].Value}'", dataBase.GetConnection());
  85. var id = sqlCommand_genre.ExecuteScalar().ToString();
  86. var deleteQuery = $"delete from Genre where id_genre = {id}";
  87. var command = new SqlCommand(deleteQuery, dataBase.GetConnection());
  88. command.ExecuteNonQuery();
  89. }
  90. }
  91. dataBase.closeConnection();
  92. }
  93. private void StrokaSearch_TextChanged(object sender, EventArgs e)
  94. {
  95. Search(dataGridView1);
  96. }
  97. private void Search(DataGridView dgw)
  98. {
  99. dgw.Rows.Clear();
  100. string searchString = $"Select genre from Genre Where genre like '%" + StrokaSearch.Text + "%'";
  101. SqlCommand com = new SqlCommand(searchString, dataBase.GetConnection());
  102. dataBase.openConnection();
  103. SqlDataReader read = com.ExecuteReader();
  104. while (read.Read())
  105. {
  106. ReadSingleRow(dgw, read);
  107. }
  108. read.Close();
  109. }
  110. private void pictureBox1_Click(object sender, EventArgs e)
  111. {
  112. RefresDataGird(dataGridView1);
  113. save = false;
  114. }
  115. private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
  116. {
  117. dataBase.openConnection();
  118. var selectedRow = e.RowIndex;
  119. if (e.RowIndex >= 0)
  120. {
  121. DataGridViewRow row = dataGridView1.Rows[selectedRow];
  122. genre = row.Cells[0].Value.ToString();
  123. SqlCommand sqlCommand_genre = new SqlCommand($"SELECT id_genre From Genre WHERE genre = '{genre}'", dataBase.GetConnection());
  124. id_genre = sqlCommand_genre.ExecuteScalar().ToString();
  125. }
  126. dataBase.closeConnection();
  127. }
  128. private void button_insert_Click(object sender, EventArgs e)
  129. {
  130. if (id_genre == null)
  131. {
  132. MessageBox.Show("Не выделена строчка для изменения");
  133. }
  134. else
  135. {
  136. this.Hide();
  137. Genre_update genre_Update = new Genre_update(id_genre, genre);
  138. genre_Update.ShowDialog();
  139. }
  140. }
  141. private void Genre_Load(object sender, EventArgs e)
  142. {
  143. CreateColumns();
  144. RefresDataGird(dataGridView1);
  145. }
  146. private void button_delete_Click_1(object sender, EventArgs e)
  147. {
  148. dataBase.openConnection();
  149. string admin = $"SELECT P.id_product From Genre G inner join Products P ON G.id_genre = P.id_genre WHERE G.genre = '{genre}'";
  150. SqlDataAdapter sda = new SqlDataAdapter(admin, dataBase.GetConnection());
  151. DataTable dtbl = new DataTable();
  152. sda.Fill(dtbl);
  153. if (dtbl.Rows.Count < 1)
  154. {
  155. if (id_genre == null)
  156. {
  157. MessageBox.Show("Не выделена запись для удаления!!!");
  158. }
  159. else
  160. {
  161. deleteRow();
  162. save = true;
  163. }
  164. }
  165. else
  166. {
  167. MessageBox.Show("Нельзя удалить запись, на которую есть ссылка!!!");
  168. }
  169. dataBase.closeConnection();
  170. }
  171. private void button_Save_Click(object sender, EventArgs e)
  172. {
  173. if (save == true)
  174. {
  175. Update();
  176. save = false;
  177. }
  178. }
  179. }
  180. }