Number.cs 6.2 KB

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