Main.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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, surname, patronymic, number, group_type, codeCard;
  23. DataBase dataBase = new DataBase();
  24. public Main()
  25. {
  26. InitializeComponent();
  27. }
  28. private void ReadSingleRow(DataGridView dgw, IDataRecord record)
  29. {
  30. dgw.Rows.Add(record.GetInt32(0), record.GetString(1), record.GetString(2), record.GetString(3), record.GetString(4), record.GetString(5), record.GetString(6));
  31. }
  32. private void RefresDataGird(DataGridView dgw)
  33. {
  34. dgw.Rows.Clear();
  35. string queryString = $"Select * from Student";
  36. SqlCommand command = new SqlCommand(queryString, dataBase.GetConnection());
  37. dataBase.openConnection();
  38. SqlDataReader reader = command.ExecuteReader();
  39. while (reader.Read())
  40. {
  41. ReadSingleRow(dgw, reader);
  42. }
  43. reader.Close();
  44. }
  45. private void CreateColumns()
  46. {
  47. dataGridView1.Columns.Add("code", "ID");
  48. dataGridView1.Columns.Add("name", "Имя");
  49. dataGridView1.Columns.Add("surname", "Фамилия");
  50. dataGridView1.Columns.Add("patronymic", "Отчество");
  51. dataGridView1.Columns.Add("number", "Телефон");
  52. dataGridView1.Columns.Add("group_type", "Группа");
  53. dataGridView1.Columns.Add("codeCard", "Код карты");
  54. dataGridView1.Columns.Add("", "");
  55. }
  56. private void Main_FormClosed(object sender, FormClosedEventArgs e)
  57. {
  58. Application.Exit();
  59. }
  60. private void Main_Load(object sender, EventArgs e)
  61. {
  62. CreateColumns();
  63. RefresDataGird(dataGridView1);
  64. dataBase.openConnection();
  65. button2.Visible = true;
  66. button4.Visible = true;
  67. }
  68. private void button2_Click(object sender, EventArgs e)
  69. {
  70. deleteRow();
  71. Update();
  72. }
  73. private void deleteRow()
  74. {
  75. int index = dataGridView1.CurrentCell.RowIndex;
  76. dataGridView1.Rows[index].Visible = false;
  77. if (dataGridView1.Rows[index].Cells[0].Value.ToString() == String.Empty)
  78. {
  79. dataGridView1.Rows[index].Cells[6].Value = RowState.Deleted;
  80. return;
  81. }
  82. dataGridView1.Rows[index].Cells[6].Value = RowState.Deleted;
  83. }
  84. new private void Update()
  85. {
  86. dataBase.openConnection();
  87. for (int index = 0; index < dataGridView1.Rows.Count; index++)
  88. {
  89. try
  90. {
  91. var rowState = (RowState)dataGridView1.Rows[index].Cells[6].Value;
  92. if (rowState == RowState.Existed)
  93. continue;
  94. if (rowState == RowState.Deleted)
  95. {
  96. var id = Convert.ToInt32(dataGridView1.Rows[index].Cells[0].Value);
  97. var deleteQuery = $"delete from Student where code = {id}";
  98. var command = new SqlCommand(deleteQuery, dataBase.GetConnection());
  99. command.ExecuteNonQuery();
  100. }
  101. }
  102. catch { }
  103. }
  104. dataBase.closeConnection();
  105. }
  106. private void button4_Click(object sender, EventArgs e)
  107. {
  108. this.Hide();
  109. DannyeAdd add = new DannyeAdd();
  110. add.Show();
  111. }
  112. private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
  113. {
  114. var selectedRow = e.RowIndex;
  115. if (e.RowIndex >= 0)
  116. {
  117. DataGridViewRow row = dataGridView1.Rows[selectedRow];
  118. code = row.Cells[0].Value.ToString();
  119. name = row.Cells[1].Value.ToString();
  120. surname = row.Cells[2].Value.ToString();
  121. patronymic = row.Cells[3].Value.ToString();
  122. number = row.Cells[4].Value.ToString();
  123. group_type = row.Cells[5].Value.ToString();
  124. codeCard = row.Cells[6].Value.ToString();
  125. }
  126. }
  127. }
  128. }