Postavshiki.cs 7.9 KB

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