Main.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. // Проверка является ли пользователь администратором, если да – открывается доступ к 2 двум кнопка работы над таблицей справочника
  66. SqlCommand sqlCommand2 = new SqlCommand($"SELECT administrator From Employee WHERE login = '{proverkaLogin}'", dataBase.GetConnection());
  67. string admPolzovatel = sqlCommand2.ExecuteScalar().ToString();
  68. if (admPolzovatel.Trim() == "true")
  69. {
  70. button2.Visible = true;
  71. button4.Visible = true;
  72. }
  73. }
  74. private void pictureBox1_Click(object sender, EventArgs e)
  75. {
  76. Profile profile = new Profile();
  77. profile.Show();
  78. }
  79. private void button2_Click(object sender, EventArgs e)
  80. {
  81. deleteRow();
  82. Update();
  83. }
  84. private void deleteRow()
  85. {
  86. int index = dataGridView1.CurrentCell.RowIndex;
  87. dataGridView1.Rows[index].Visible = false;
  88. if (dataGridView1.Rows[index].Cells[0].Value.ToString() == String.Empty)
  89. {
  90. dataGridView1.Rows[index].Cells[5].Value = RowState.Deleted;
  91. return;
  92. }
  93. dataGridView1.Rows[index].Cells[5].Value = RowState.Deleted;
  94. }
  95. new private void Update()
  96. {
  97. dataBase.openConnection();
  98. for (int index = 0; index < dataGridView1.Rows.Count; index++)
  99. {
  100. try
  101. {
  102. var rowState = (RowState)dataGridView1.Rows[index].Cells[5].Value;
  103. if (rowState == RowState.Existed)
  104. continue;
  105. if (rowState == RowState.Deleted)
  106. {
  107. var id = Convert.ToInt32(dataGridView1.Rows[index].Cells[0].Value);
  108. var deleteQuery = $"delete from Dannye where code = {id}";
  109. var command = new SqlCommand(deleteQuery, dataBase.GetConnection());
  110. command.ExecuteNonQuery();
  111. }
  112. }
  113. catch { }
  114. }
  115. dataBase.closeConnection();
  116. }
  117. private void button4_Click(object sender, EventArgs e)
  118. {
  119. this.Hide();
  120. DannyeAdd add = new DannyeAdd();
  121. add.Show();
  122. }
  123. private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
  124. {
  125. var selectedRow = e.RowIndex;
  126. if (e.RowIndex >= 0)
  127. {
  128. DataGridViewRow row = dataGridView1.Rows[selectedRow];
  129. code = row.Cells[0].Value.ToString();
  130. name_dan = row.Cells[1].Value.ToString();
  131. adres_dan = row.Cells[2].Value.ToString();
  132. city_dan = row.Cells[3].Value.ToString();
  133. number_dan = row.Cells[4].Value.ToString();
  134. contact_dan = row.Cells[5].Value.ToString();
  135. }
  136. }
  137. }
  138. }