革博士V2程序
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

UserListFrm.cs 6.3 KiB

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