Country.cs 6.5 KB

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