版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

158 строки
6.5 KiB

  1. using Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace ProductionControl
  12. {
  13. public partial class FrmUserList : Form
  14. {
  15. Service.UserService service = new Service.UserService();
  16. public FrmUserList()
  17. {
  18. InitializeComponent();
  19. dataGridView1.AutoGenerateColumns = false;
  20. }
  21. private void initDataView()
  22. {
  23. var list = service.GetListNav();
  24. tsslCount.Text = $"共 {list.Count} 条记录";
  25. dataGridView1.DataSource = new BindingSource(list, null);
  26. }
  27. private void FrmUserList_Load(object sender, EventArgs e)
  28. {
  29. initDataView();
  30. }
  31. private void tsbtnAdd_Click(object sender, EventArgs e)
  32. {
  33. FrmUserInfo frm = new FrmUserInfo();
  34. if (frm.ShowDialog() == DialogResult.OK)
  35. initDataView();
  36. }
  37. private void tsbtnDel_Click(object sender, EventArgs e)
  38. {
  39. try
  40. {
  41. if (this.dataGridView1.CurrentRow == null)
  42. return;
  43. var list = ((BindingSource)dataGridView1.DataSource).DataSource as List<User>;
  44. int liIndex = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引
  45. if (list[liIndex].Code=="admin")
  46. throw new Exception("管理员不可删除!");
  47. if (MessageBox.Show($"确认删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  48. {
  49. if (!service.Delete(list[liIndex]))
  50. throw new Exception("删除失败!");
  51. MessageBox.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  52. initDataView();
  53. }
  54. }
  55. catch (Exception ex)
  56. {
  57. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  58. }
  59. }
  60. private void tsbtnEnable_Click(object sender, EventArgs e)
  61. {
  62. try
  63. {
  64. if (this.dataGridView1.CurrentRow == null)
  65. return;
  66. var list = ((BindingSource)dataGridView1.DataSource).DataSource as List<User>;
  67. int liIndex = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引
  68. if (MessageBox.Show($"确认启用?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  69. {
  70. list[liIndex].State = true;
  71. //只更新指定列
  72. if (!service.Update(it => new User() { State = list[liIndex].State }, it => it.Id == list[liIndex].Id))
  73. throw new Exception("启用失败!");
  74. MessageBox.Show("启用成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  75. initDataView();
  76. }
  77. }
  78. catch (Exception ex)
  79. {
  80. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  81. }
  82. }
  83. private void tsbtnDisable_Click(object sender, EventArgs e)
  84. {
  85. try
  86. {
  87. if (this.dataGridView1.CurrentRow == null)
  88. return;
  89. var list = ((BindingSource)dataGridView1.DataSource).DataSource as List<User>;
  90. int liIndex = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引
  91. if (list[liIndex].Code=="admin")
  92. throw new Exception("管理员不可禁用!");
  93. if (MessageBox.Show($"确认禁用?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  94. {
  95. list[liIndex].State = false;
  96. //只更新指定列
  97. if (!service.Update(it => new User() { State = list[liIndex].State }, it => it.Id == list[liIndex].Id))
  98. throw new Exception("禁用失败!");
  99. MessageBox.Show("禁用成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  100. initDataView();
  101. }
  102. }
  103. catch (Exception ex)
  104. {
  105. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  106. }
  107. }
  108. private void tsbtnResetPW_Click(object sender, EventArgs e)
  109. {
  110. if (this.dataGridView1.CurrentRow == null)
  111. return;
  112. var list = ((BindingSource)dataGridView1.DataSource).DataSource as List<User>;
  113. int liIndex = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引
  114. if (MessageBox.Show($"确认重置密码?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  115. {
  116. string szUserPw = Utils.Util.GetMD5("");
  117. list[liIndex].Password = szUserPw;
  118. //只更新指定列
  119. if (!service.Update(it => new User() { Password = list[liIndex].Password }, it => it.Id == list[liIndex].Id))
  120. throw new Exception("重置失败!");
  121. MessageBox.Show("重置成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  122. initDataView();
  123. }
  124. }
  125. private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  126. {
  127. var list = ((BindingSource)dataGridView1.DataSource).DataSource as List<User>;
  128. int liIndex = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引
  129. FrmUserInfo frm = new FrmUserInfo(list[liIndex]);
  130. frm.ShowDialog(this);
  131. initDataView();
  132. }
  133. private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
  134. {
  135. var list = ((BindingSource)dataGridView1.DataSource).DataSource as List<User>;
  136. for (int i = 0; i < dataGridView1.Rows.Count; i++)
  137. {
  138. if (list[i].RoleInfo != null)
  139. dataGridView1.Rows[i].Cells["colRoleName"].Value = list[i].RoleInfo.Name;
  140. }
  141. }
  142. }
  143. }