Nomenklatura.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 Nomenklatura : Form
  14. {
  15. string id_product;
  16. string product;
  17. string description;
  18. string price;
  19. string year_publishing;
  20. string country;
  21. string edition;
  22. string genre;
  23. string performer;
  24. string supplier;
  25. bool save;
  26. DataBase dataBase = new DataBase();
  27. public Nomenklatura()
  28. {
  29. InitializeComponent();
  30. }
  31. private void CreateColumns()
  32. {
  33. dataGridView1.Columns.Add("product", "Номенклатура");
  34. dataGridView1.Columns.Add("description", "Описание");
  35. dataGridView1.Columns.Add("price", "Цена");
  36. dataGridView1.Columns.Add("year_publishing", "Год издания");
  37. dataGridView1.Columns.Add("country", "Страна производства");
  38. dataGridView1.Columns.Add("edition", "Издательство");
  39. dataGridView1.Columns.Add("genre", "Жанр музыки");
  40. dataGridView1.Columns.Add("performer", "Исполнитель");
  41. dataGridView1.Columns.Add("supplier", "Поставщик");
  42. dataGridView1.Columns.Add("", String.Empty);
  43. }
  44. private void ReadSingleRow(DataGridView dgw, IDataRecord record)
  45. {
  46. dgw.Rows.Add(record.GetString(0), record.GetString(1), record.GetDouble(2), record.GetString(3), record.GetString(4), record.GetString(5), record.GetString(6), record.GetString(7), record.GetString(8), RowState.ModifiedNew);
  47. }
  48. private void RefresDataGird(DataGridView dgw)
  49. {
  50. dgw.Rows.Clear();
  51. dataGridView1.Columns[9].Visible = false;
  52. string queryString = $"Select P.product, P.description, P.price, P.year_publishing, C.country, E.edition, G.genre, Per.performer, S.name_organization From Products P inner join Country C ON P.id_country = C.id_country inner join Edition E ON P.id_edition = E.id_edition inner join Genre G ON P.id_genre = G.id_genre inner join Performer Per ON P.id_performer = Per.id_performer inner join Supplier S ON P.id_supplier = S.id_supplier";
  53. SqlCommand command = new SqlCommand(queryString, dataBase.GetConnection());
  54. dataBase.openConnection();
  55. SqlDataReader reader = command.ExecuteReader();
  56. while (reader.Read())
  57. {
  58. ReadSingleRow(dgw, reader);
  59. }
  60. reader.Close();
  61. }
  62. private void Nomenklatura_Load(object sender, EventArgs e)
  63. {
  64. CreateColumns();
  65. RefresDataGird(dataGridView1);
  66. }
  67. private void button_back_Click(object sender, EventArgs e)
  68. {
  69. this.Hide();
  70. MainMenu mainMenu = new MainMenu();
  71. mainMenu.ShowDialog();
  72. }
  73. public void global_FormClosed(object sender, EventArgs e)
  74. {
  75. Application.Exit();
  76. }
  77. private void pictureBox2_Click(object sender, EventArgs e)
  78. {
  79. RefresDataGird(dataGridView1);
  80. save = false;
  81. }
  82. private void button_add_Click(object sender, EventArgs e)
  83. {
  84. this.Hide();
  85. Nomenklaturaadd nomenklatura_Add = new Nomenklaturaadd();
  86. nomenklatura_Add.ShowDialog();
  87. }
  88. private void Search(DataGridView dgw)
  89. {
  90. dgw.Rows.Clear();
  91. string searchString = $"Select P.product, P.description, P.price, P.year_publishing, C.country, E.edition, G.genre, Per.performer, S.name_organization From Products P inner join Country C ON P.id_country = C.id_country inner join Edition E ON P.id_edition = E.id_edition inner join Genre G ON P.id_genre = G.id_genre inner join Performer Per ON P.id_performer = Per.id_performer inner join Supplier S ON P.id_supplier = S.id_supplier Where product like '%" + StrokaSearch.Text +"%'";
  92. SqlCommand com = new SqlCommand(searchString, dataBase.GetConnection());
  93. dataBase.openConnection();
  94. SqlDataReader read = com.ExecuteReader();
  95. while (read.Read())
  96. {
  97. ReadSingleRow(dgw, read);
  98. }
  99. read.Close();
  100. }
  101. private void deleteRow()
  102. {
  103. int index = dataGridView1.CurrentCell.RowIndex;
  104. dataGridView1.Rows[index].Visible = false;
  105. if(dataGridView1.Rows[index].Cells[0].Value.ToString() == String.Empty)
  106. {
  107. dataGridView1.Rows[index].Cells[9].Value = RowState.Deleted;
  108. return;
  109. }
  110. dataGridView1.Rows[index].Cells[9].Value = RowState.Deleted;
  111. id_product = null;
  112. }
  113. new private void Update()
  114. {
  115. dataBase.openConnection();
  116. for(int index = 0; index < dataGridView1.Rows.Count; index++)
  117. {
  118. var rowState = (RowState)dataGridView1.Rows[index].Cells[9].Value;
  119. if (rowState == RowState.Existed)
  120. continue;
  121. if(rowState == RowState.Deleted)
  122. {
  123. SqlCommand sqlCommand_product = new SqlCommand($"SELECT id_product From Products WHERE product = '{dataGridView1.Rows[index].Cells[0].Value}'", dataBase.GetConnection());
  124. var id = sqlCommand_product.ExecuteScalar().ToString();
  125. var deleteQuery = $"delete from Products where id_product = {id}";
  126. var command = new SqlCommand(deleteQuery, dataBase.GetConnection());
  127. command.ExecuteNonQuery();
  128. }
  129. }
  130. dataBase.closeConnection();
  131. }
  132. private void StrokaSearch_TextChanged(object sender, EventArgs e)
  133. {
  134. Search(dataGridView1);
  135. }
  136. private void button_delete_Click(object sender, EventArgs e)
  137. {
  138. dataBase.openConnection();
  139. string admin = $"SELECT S.id_sale From Sales S inner join Products P ON S.id_product = P.id_product WHERE P.product = '{product}'";
  140. SqlDataAdapter sda = new SqlDataAdapter(admin, dataBase.GetConnection());
  141. DataTable dtbl = new DataTable();
  142. sda.Fill(dtbl);
  143. if (dtbl.Rows.Count < 1)
  144. {
  145. if(id_product == null)
  146. {
  147. MessageBox.Show("Не выделена запись для удаления!!!");
  148. }
  149. else
  150. {
  151. deleteRow();
  152. save = true;
  153. }
  154. }
  155. else
  156. {
  157. MessageBox.Show("Нельзя удалить запись, на которую есть ссылка!!!");
  158. }
  159. dataBase.closeConnection();
  160. }
  161. private void buttonSave_Click(object sender, EventArgs e)
  162. {
  163. if (save == true)
  164. {
  165. Update();
  166. save = false;
  167. }
  168. }
  169. private void button_insert_Click(object sender, EventArgs e)
  170. {
  171. if(id_product == null)
  172. {
  173. MessageBox.Show("Не выделена строчка для изменения");
  174. }
  175. else
  176. {
  177. this.Hide();
  178. Nomenklaturaupdate nomenklatura_Update = new Nomenklaturaupdate(id_product, product, description, price, year_publishing, country, edition, genre, performer, supplier);
  179. nomenklatura_Update.ShowDialog();
  180. }
  181. }
  182. public void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
  183. {
  184. var selectedRow = e.RowIndex;
  185. if (e.RowIndex >= 0)
  186. {
  187. DataGridViewRow row = dataGridView1.Rows[selectedRow];
  188. dataBase.openConnection();
  189. product = row.Cells[0].Value.ToString();
  190. description = row.Cells[1].Value.ToString();
  191. price = row.Cells[2].Value.ToString();
  192. year_publishing = row.Cells[3].Value.ToString();
  193. country = row.Cells[4].Value.ToString();
  194. edition = row.Cells[5].Value.ToString();
  195. genre = row.Cells[6].Value.ToString();
  196. performer = row.Cells[7].Value.ToString();
  197. supplier = row.Cells[8].Value.ToString();
  198. SqlCommand sqlCommand_product = new SqlCommand($"SELECT id_product From Products WHERE product = '{product}'", dataBase.GetConnection());
  199. id_product = sqlCommand_product.ExecuteScalar().ToString();
  200. dataBase.closeConnection();
  201. }
  202. }
  203. }
  204. }