AdminForm.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 Session_4
  12. {
  13. public partial class AdminForm : Form
  14. {
  15. SqlConnection connection;
  16. SqlCommand command;
  17. SqlDataAdapter adapter;
  18. DataTable table;
  19. public AdminForm()
  20. {
  21. InitializeComponent();
  22. dataGridView1.AutoGenerateColumns = true;
  23. connection = new SqlConnection("Server=class31000;Database=Session3_4;Trusted_Connection=True;");
  24. command = new SqlCommand();
  25. command.Connection = connection;
  26. command.CommandType = CommandType.Text;
  27. adapter = new SqlDataAdapter(command);
  28. table = new DataTable();
  29. dataGridView1.DataSource = table;
  30. }
  31. private void AdminForm_Load(object sender, EventArgs e)
  32. {
  33. // TODO: данная строка кода позволяет загрузить данные в таблицу "session3_4DataSet.Users". При необходимости она может быть перемещена или удалена.
  34. this.usersTableAdapter.Fill(this.session3_4DataSet.Users);
  35. // TODO: данная строка кода позволяет загрузить данные в таблицу "session3_4DataSet.Offices". При необходимости она может быть перемещена или удалена.
  36. this.officesTableAdapter.Fill(this.session3_4DataSet.Offices);
  37. ShowTable("SELECT Users.ID, Users.FirstName AS Name, Users.LastName, DATEDIFF(YEAR, Users.Birthdate, GETDATE()) AS Age, Roles.Title AS[User Role], Users.Email AS[Email Address], Offices.Title AS Office, Users.Active FROM Users INNER JOIN Roles ON Roles.ID = Users.RoleID INNER JOIN Offices ON Users.OfficeID = Offices.ID");
  38. comboBox1.Text = "All Offices";
  39. dataGridView1.Columns[7].Visible = false;
  40. PaintRows();
  41. }
  42. public void ShowTable(string text)
  43. {
  44. dataGridView1.Columns.Clear();
  45. dataGridView1.DataSource = null;
  46. command.CommandText = text;
  47. table.Clear();
  48. adapter.Fill(table);
  49. dataGridView1.DataSource = table;
  50. }
  51. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  52. {
  53. ShowTable("SELECT Users.ID, Users.FirstName AS Name, Users.LastName, DATEDIFF(YEAR, Users.Birthdate, GETDATE()) AS Age, Roles.Title AS[User Role], Users.Email AS[Email Address], Offices.Title AS Office, Users.Active FROM Users INNER JOIN Roles ON Roles.ID = Users.RoleID INNER JOIN Offices ON Users.OfficeID = Offices.ID WHERE Offices.Title = (\'" + comboBox1.Text + "\')");
  54. all_offices_button.Visible = true;
  55. dataGridView1.Columns[7].Visible = false;
  56. PaintRows();
  57. }
  58. private void all_offices_button_Click(object sender, EventArgs e)
  59. {
  60. ShowTable("SELECT Users.ID, Users.FirstName AS Name, Users.LastName, DATEDIFF(YEAR, Users.Birthdate, GETDATE()) AS Age, Roles.Title AS[User Role], Users.Email AS[Email Address], Offices.Title AS Office, Users.Active FROM Users INNER JOIN Roles ON Roles.ID = Users.RoleID INNER JOIN Offices ON Users.OfficeID = Offices.ID");
  61. all_offices_button.Visible = false;
  62. comboBox1.Text = "All Offices";
  63. dataGridView1.Columns[7].Visible = false;
  64. PaintRows();
  65. }
  66. private void enable_disable_button_Click(object sender, EventArgs e)
  67. {
  68. if ((bool)dataGridView1.CurrentRow.Cells[7].Value == true)
  69. {
  70. dataGridView1.CurrentRow.Cells[7].Value = false;
  71. connection.Open();
  72. command.CommandText = "UPDATE Users SET Active='False' WHERE Users.ID = (\'" + dataGridView1.CurrentRow.Cells[0].Value + "\');";
  73. command.ExecuteReader();
  74. connection.Close();
  75. PaintRows();
  76. }
  77. else
  78. {
  79. dataGridView1.CurrentRow.Cells[7].Value = true;
  80. PaintRows();
  81. connection.Open();
  82. command.CommandText = "UPDATE Users SET Users.Active='True' WHERE Users.ID = (\'" + dataGridView1.CurrentRow.Cells[0].Value + "\');";
  83. command.ExecuteReader();
  84. connection.Close();
  85. PaintRows();
  86. }
  87. }
  88. private void PaintRows()
  89. {
  90. foreach (DataGridViewRow row in dataGridView1.Rows)
  91. {
  92. try
  93. {
  94. if ((bool)row.Cells[7].Value == true)
  95. row.DefaultCellStyle.BackColor = Color.LightGreen;
  96. else
  97. row.DefaultCellStyle.BackColor = Color.Red;
  98. }
  99. catch
  100. {
  101. }
  102. }
  103. }
  104. private void exit_button_Click(object sender, EventArgs e)
  105. {
  106. this.Close();
  107. Authorization authorization = new Authorization();
  108. authorization.Show();
  109. }
  110. private void add_button_Click(object sender, EventArgs e)
  111. {
  112. AdduserForm adduser = new AdduserForm();
  113. adduser.ShowDialog();
  114. }
  115. private void change_role_button_Click(object sender, EventArgs e)
  116. {
  117. EditRoleForm edit = new EditRoleForm();
  118. edit.email_text.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
  119. edit.firstname_text.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
  120. edit.lastname_text.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
  121. edit.officebox_text.SelectedItem = dataGridView1.CurrentRow.Cells[6].Value;
  122. if (dataGridView1.CurrentRow.Cells[4].Value.ToString() =="User")
  123. edit.radioButton1.Checked = true;
  124. else
  125. edit.radioButton2.Checked = true;
  126. edit.ShowDialog();
  127. }
  128. }
  129. }