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