Main.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 project
  12. {
  13. enum RowState
  14. {
  15. Existed,
  16. New,
  17. ModifiedNew,
  18. Deleted
  19. }
  20. public partial class Main : Form
  21. {
  22. string code, name_dan, adres_dan, city_dan, number_dan, contact_dan;
  23. public static string login;
  24. DataBase dataBase = new DataBase();
  25. public Main()
  26. {
  27. InitializeComponent();
  28. }
  29. private void ReadSingleRow(DataGridView dgw, IDataRecord record)
  30. {
  31. dgw.Rows.Add(record.GetInt32(0), record.GetString(1), record.GetString(2), record.GetString(3), record.GetString(4), record.GetString(5));
  32. }
  33. private void RefresDataGird(DataGridView dgw)
  34. {
  35. dgw.Rows.Clear();
  36. string queryString = $"Select * from Dannye";
  37. SqlCommand command = new SqlCommand(queryString, dataBase.GetConnection());
  38. dataBase.openConnection();
  39. SqlDataReader reader = command.ExecuteReader();
  40. while (reader.Read())
  41. {
  42. ReadSingleRow(dgw, reader);
  43. }
  44. reader.Close();
  45. }
  46. private void CreateColumns()
  47. {
  48. dataGridView1.Columns.Add("code", "ID");
  49. dataGridView1.Columns.Add("name_dan", "Наименование");
  50. dataGridView1.Columns.Add("adres_dan", "Адрес");
  51. dataGridView1.Columns.Add("city_dan", "Город");
  52. dataGridView1.Columns.Add("number_dan", "Телефон");
  53. dataGridView1.Columns.Add("contact_dan", "Контактная информация");
  54. }
  55. private void Main_FormClosed(object sender, FormClosedEventArgs e)
  56. {
  57. Application.Exit();
  58. }
  59. private void Main_Load(object sender, EventArgs e)
  60. {
  61. CreateColumns();
  62. RefresDataGird(dataGridView1);
  63. string proverkaLogin = Login.login;
  64. dataBase.openConnection();
  65. SqlCommand sqlCommand2 = new SqlCommand($"SELECT administrator From Employee WHERE login = '{proverkaLogin}'", dataBase.GetConnection());
  66. string admPolzovatel = sqlCommand2.ExecuteScalar().ToString();
  67. if (admPolzovatel.Trim() == "true")
  68. {
  69. button2.Visible = true;
  70. button4.Visible = true;
  71. }
  72. }
  73. private void pictureBox1_Click(object sender, EventArgs e)
  74. {
  75. Profile profile = new Profile();
  76. profile.Show();
  77. }
  78. private void button2_Click(object sender, EventArgs e)
  79. {
  80. deleteRow();
  81. Update();
  82. }
  83. private void deleteRow()
  84. {
  85. int index = dataGridView1.CurrentCell.RowIndex;
  86. dataGridView1.Rows[index].Visible = false;
  87. if (dataGridView1.Rows[index].Cells[0].Value.ToString() == String.Empty)
  88. {
  89. dataGridView1.Rows[index].Cells[5].Value = RowState.Deleted;
  90. return;
  91. }
  92. dataGridView1.Rows[index].Cells[5].Value = RowState.Deleted;
  93. }
  94. new private void Update()
  95. {
  96. dataBase.openConnection();
  97. for (int index = 0; index < dataGridView1.Rows.Count; index++)
  98. {
  99. try
  100. {
  101. var rowState = (RowState)dataGridView1.Rows[index].Cells[5].Value;
  102. if (rowState == RowState.Existed)
  103. continue;
  104. if (rowState == RowState.Deleted)
  105. {
  106. var id = Convert.ToInt32(dataGridView1.Rows[index].Cells[0].Value);
  107. var deleteQuery = $"delete from Dannye where code = {id}";
  108. var command = new SqlCommand(deleteQuery, dataBase.GetConnection());
  109. command.ExecuteNonQuery();
  110. }
  111. }
  112. catch { }
  113. }
  114. dataBase.closeConnection();
  115. }
  116. private void button4_Click(object sender, EventArgs e)
  117. {
  118. this.Hide();
  119. DannyeAdd add = new DannyeAdd();
  120. add.Show();
  121. }
  122. private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
  123. {
  124. var selectedRow = e.RowIndex;
  125. if (e.RowIndex >= 0)
  126. {
  127. DataGridViewRow row = dataGridView1.Rows[selectedRow];
  128. code = row.Cells[0].Value.ToString();
  129. name_dan = row.Cells[1].Value.ToString();
  130. adres_dan = row.Cells[2].Value.ToString();
  131. city_dan = row.Cells[3].Value.ToString();
  132. number_dan = row.Cells[4].Value.ToString();
  133. contact_dan = row.Cells[5].Value.ToString();
  134. }
  135. }
  136. }
  137. }