Prodaja.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.Data.SqlClient;
  10. using System.Windows.Forms;
  11. namespace proba
  12. {
  13. public partial class Form2 : Form
  14. {
  15. string id_sale;
  16. string product;
  17. string date_sale;
  18. string employee;
  19. string summa;
  20. bool save;
  21. DataBase dataBase = new DataBase();
  22. public Form2()
  23. {
  24. InitializeComponent();
  25. }
  26. private void CreateColumns()
  27. {
  28. dataGridView1.Columns.Add("product", "Название товара");
  29. dataGridView1.Columns.Add("date_sale", "Дата продажи");
  30. dataGridView1.Columns.Add("employee", "Сотрудник");
  31. dataGridView1.Columns.Add("summa", "Сумма продажи");
  32. dataGridView1.Columns.Add("", String.Empty);
  33. }
  34. private void ReadSingleRow(DataGridView dgw, IDataRecord record)
  35. {
  36. dgw.Rows.Add(record.GetString(0), record.GetString(1), record.GetString(2), record.GetString(3), RowState.ModifiedNew);
  37. }
  38. private void RefresDataGird(DataGridView dgw)
  39. {
  40. dgw.Rows.Clear();
  41. dataGridView1.Columns[4].Visible = false;
  42. string queryString = $"Select P.product, S.date_sale, E.surname, S.summa from Sales S inner join Products P ON P.id_product=S.id_product inner join Employee E ON S.id_employee=E.id_employee";
  43. SqlCommand command = new SqlCommand(queryString, dataBase.GetConnection());
  44. dataBase.openConnection();
  45. SqlDataReader reader = command.ExecuteReader();
  46. while (reader.Read())
  47. {
  48. ReadSingleRow(dgw, reader);
  49. }
  50. reader.Close();
  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 pictureBox2_Click(object sender, EventArgs e)
  63. {
  64. RefresDataGird(dataGridView1);
  65. }
  66. private void Search(DataGridView dgw)
  67. {
  68. dgw.Rows.Clear();
  69. string searchString = $"Select P.product, S.date_sale, E.surname, S.Summa from Sales S inner join Products P ON P.id_product=S.id_product inner join Employee E ON S.id_employee=E.id_employee Where P.product like '%" + StrokaSearch.Text + "%'";
  70. SqlCommand com = new SqlCommand(searchString, dataBase.GetConnection());
  71. dataBase.openConnection();
  72. SqlDataReader read = com.ExecuteReader();
  73. while (read.Read())
  74. {
  75. ReadSingleRow(dgw, read);
  76. }
  77. read.Close();
  78. }
  79. private void StrokaSearch_TextChanged(object sender, EventArgs e)
  80. {
  81. Search(dataGridView1);
  82. }
  83. private void Form2_Load(object sender, EventArgs e)
  84. {
  85. CreateColumns();
  86. RefresDataGird(dataGridView1);
  87. }
  88. private void deleteRow()
  89. {
  90. int index = dataGridView1.CurrentCell.RowIndex;
  91. dataGridView1.Rows[index].Visible = false;
  92. if (dataGridView1.Rows[index].Cells[0].Value.ToString() == String.Empty)
  93. {
  94. dataGridView1.Rows[index].Cells[4].Value = RowState.Deleted;
  95. return;
  96. }
  97. dataGridView1.Rows[index].Cells[4].Value = RowState.Deleted;
  98. id_sale = null;
  99. }
  100. private void button_delete_Click(object sender, EventArgs e)
  101. {
  102. if (id_sale == null)
  103. {
  104. MessageBox.Show("Не выделена запись для удаления!!!");
  105. }
  106. else
  107. {
  108. deleteRow();
  109. save = true;
  110. }
  111. }
  112. new private void Update()
  113. {
  114. dataBase.openConnection();
  115. for (int index = 0; index < dataGridView1.Rows.Count; index++)
  116. {
  117. var rowState = (RowState)dataGridView1.Rows[index].Cells[4].Value;
  118. if (rowState == RowState.Existed)
  119. continue;
  120. if (rowState == RowState.Deleted)
  121. {
  122. SqlCommand sqlCommand_product = new SqlCommand($"SELECT id_sale From Sales S inner join Products P ON S.id_product=P.id_product WHERE P.product = '{dataGridView1.Rows[index].Cells[0].Value}'", dataBase.GetConnection());
  123. var id = sqlCommand_product.ExecuteScalar().ToString();
  124. var deleteQuery = $"delete from Sales where id_sale = {id}";
  125. var command = new SqlCommand(deleteQuery, dataBase.GetConnection());
  126. command.ExecuteNonQuery();
  127. }
  128. }
  129. dataBase.closeConnection();
  130. }
  131. private void buttonSave_Click(object sender, EventArgs e)
  132. {
  133. if (save == true)
  134. {
  135. Update();
  136. save = false;
  137. }
  138. }
  139. private void button_insert_Click(object sender, EventArgs e)
  140. {
  141. if (id_sale == null)
  142. {
  143. MessageBox.Show("Не выделена строчка для изменения");
  144. }
  145. else
  146. {
  147. this.Hide();
  148. Prodaja_update prodaja_Update = new Prodaja_update(id_sale, product, date_sale, employee, summa);
  149. prodaja_Update.ShowDialog();
  150. }
  151. }
  152. private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
  153. {
  154. var selectedRow = e.RowIndex;
  155. if (e.RowIndex >= 0)
  156. {
  157. DataGridViewRow row = dataGridView1.Rows[selectedRow];
  158. product = row.Cells[0].Value.ToString();
  159. date_sale = row.Cells[1].Value.ToString();
  160. employee = row.Cells[2].Value.ToString();
  161. summa = row.Cells[3].Value.ToString();
  162. dataBase.openConnection();
  163. SqlCommand sqlCommand_product = new SqlCommand($"SELECT id_sale From Sales S inner join Products P ON S.id_product=P.id_product WHERE P.product = '{product}'", dataBase.GetConnection());
  164. id_sale = sqlCommand_product.ExecuteScalar().ToString();
  165. dataBase.closeConnection();
  166. }
  167. }
  168. private void pictureBox2_Click_1(object sender, EventArgs e)
  169. {
  170. RefresDataGird(dataGridView1);
  171. save = false;
  172. }
  173. private void button_add_Click(object sender, EventArgs e)
  174. {
  175. this.Hide();
  176. Sales sales = new Sales();
  177. sales.ShowDialog();
  178. }
  179. private void button_saleDay_Click(object sender, EventArgs e)
  180. {
  181. this.Hide();
  182. SummaSaleDay summaSaleDay = new SummaSaleDay();
  183. summaSaleDay.ShowDialog();
  184. }
  185. }
  186. }