SADD.cs 6.6 KB

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