Organizations.cs 7.1 KB

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