Performer.cs 6.9 KB

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