SubscriptionFee.cs 6.6 KB

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