ADD_customers.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 ADD_customers : Form
  14. {
  15. string id_num;
  16. string name1;
  17. string name2;
  18. bool save;
  19. public ADD_customers() // ДВО абонентов
  20. {
  21. InitializeComponent();
  22. }
  23. DataBase dataBase = new DataBase();
  24. private void CreateColums()
  25. {
  26. dataGridView1.Columns.Add("id", "id");
  27. dataGridView1.Columns.Add("Name", "Название организации (абонента)");
  28. dataGridView1.Columns.Add("name", "Название ДВО");
  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.GetString(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 ADD_customers.ADD_code, Organizations.Name, ServicesADD.Name from Organizations join ADD_customers on Organizations.Organizations_code=ADD_customers.Organizations_code join ServicesADD on ServicesADD.ServicesADD_code=ADD_customers.ServicesADD_code";
  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 dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) // выбор
  51. {
  52. dataBase.openConnection();
  53. var selectedRow = e.RowIndex;
  54. if (e.RowIndex >= 0)
  55. {
  56. DataGridViewRow row = dataGridView1.Rows[selectedRow];
  57. id_num = row.Cells[0].Value.ToString();
  58. name1 = row.Cells[1].Value.ToString();
  59. name2 = row.Cells[2].Value.ToString();
  60. SqlCommand sqlCommand_country = new SqlCommand($"SELECT ADD_code from ADD_customers WHERE ADD_code = '{id_num}'", dataBase.GetConnection());
  61. id_num = sqlCommand_country.ExecuteScalar().ToString();
  62. }
  63. dataBase.closeConnection();
  64. }
  65. private void InternetADD_button_Click(object sender, EventArgs e)
  66. {
  67. this.Hide();
  68. ADD_customersADD IDD = new ADD_customersADD();
  69. IDD.ShowDialog();
  70. }
  71. private void UPD_button_Click(object sender, EventArgs e) // изменение
  72. {
  73. if (id_num == null)
  74. {
  75. MessageBox.Show("Не выделена строчка для изменения");
  76. }
  77. else
  78. {
  79. this.Hide();
  80. ADD_customersUPD employeeUpdate = new ADD_customersUPD(id_num, name1, name2);
  81. employeeUpdate.ShowDialog();
  82. }
  83. }
  84. private void StrokaSearch_TextChanged(object sender, EventArgs e) // поиск
  85. {
  86. Search(dataGridView1);
  87. }
  88. private void Search(DataGridView dgw)
  89. {
  90. dgw.Rows.Clear();
  91. string searchString = $"select ADD_customers.ADD_code, Organizations.Name, ServicesADD.Name from Organizations join ADD_customers on Organizations.Organizations_code=ADD_customers.Organizations_code join ServicesADD on ServicesADD.ServicesADD_code=ADD_customers.ServicesADD_code where Organizations.Name like '%" + StrokaSearch.Text + "%'";
  92. SqlCommand com = new SqlCommand(searchString, dataBase.GetConnection());
  93. dataBase.openConnection();
  94. SqlDataReader read = com.ExecuteReader();
  95. while (read.Read())
  96. {
  97. ReadSingleRow(dgw, read);
  98. }
  99. read.Close();
  100. }
  101. private void ADD_customers_Load(object sender, EventArgs e) // загрузка
  102. {
  103. CreateColums();
  104. RefrashDataGrid(dataGridView1);
  105. }
  106. private void button_back_Click(object sender, EventArgs e)
  107. {
  108. this.Hide();
  109. Menu M = new Menu();
  110. M.ShowDialog();
  111. }
  112. private void Del_button_Click(object sender, EventArgs e)
  113. {
  114. if (id_num == null)
  115. {
  116. MessageBox.Show("Не выделена запись для удаления!!!");
  117. }
  118. else
  119. {
  120. deleteRow();
  121. save = true;
  122. }
  123. }
  124. private void deleteRow()
  125. {
  126. int index = dataGridView1.CurrentCell.RowIndex;
  127. dataGridView1.Rows[index].Visible = false;
  128. if (dataGridView1.Rows[index].Cells[0].Value.ToString() == String.Empty)
  129. {
  130. dataGridView1.Rows[index].Cells[3].Value = RowState.Deleted;
  131. return;
  132. }
  133. dataGridView1.Rows[index].Cells[3].Value = RowState.Deleted;
  134. }
  135. new private void Update()
  136. {
  137. dataBase.openConnection();
  138. for (int index = 0; index < dataGridView1.Rows.Count; index++)
  139. {
  140. var rowState = (RowState)dataGridView1.Rows[index].Cells[3].Value;
  141. if (rowState == RowState.Existed)
  142. continue;
  143. if (rowState == RowState.Deleted)
  144. {
  145. var deleteQuery = $"delete from ADD_customers where ADD_code = '{id_num}'";
  146. var command = new SqlCommand(deleteQuery, dataBase.GetConnection());
  147. command.ExecuteNonQuery();
  148. }
  149. }
  150. dataBase.closeConnection();
  151. }
  152. private void button1_Click(object sender, EventArgs e)
  153. {
  154. if (save == true)
  155. {
  156. Update();
  157. save = false;
  158. }
  159. }
  160. private void global_FormClosed(object sender, FormClosedEventArgs e)
  161. {
  162. Application.Exit();
  163. }
  164. }
  165. }