Services_customers.cs 8.3 KB

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