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