using Models; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ProductionControl { public partial class FrmUserList : Form { Service.UserService service = new Service.UserService(); public FrmUserList() { InitializeComponent(); dataGridView1.AutoGenerateColumns = false; } private void initDataView() { var list = service.GetListNav(); tsslCount.Text = $"共 {list.Count} 条记录"; dataGridView1.DataSource = new BindingSource(list, null); } private void FrmUserList_Load(object sender, EventArgs e) { initDataView(); } private void tsbtnAdd_Click(object sender, EventArgs e) { FrmUserInfo frm = new FrmUserInfo(); if (frm.ShowDialog() == DialogResult.OK) initDataView(); } private void tsbtnDel_Click(object sender, EventArgs e) { try { if (this.dataGridView1.CurrentRow == null) return; var list = ((BindingSource)dataGridView1.DataSource).DataSource as List; int liIndex = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引 if (list[liIndex].Code=="admin") throw new Exception("管理员不可删除!"); if (MessageBox.Show($"确认删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { if (!service.Delete(list[liIndex])) throw new Exception("删除失败!"); MessageBox.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); initDataView(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void tsbtnEnable_Click(object sender, EventArgs e) { try { if (this.dataGridView1.CurrentRow == null) return; var list = ((BindingSource)dataGridView1.DataSource).DataSource as List; int liIndex = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引 if (MessageBox.Show($"确认启用?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { list[liIndex].State = true; //只更新指定列 if (!service.Update(it => new User() { State = list[liIndex].State }, it => it.Id == list[liIndex].Id)) throw new Exception("启用失败!"); MessageBox.Show("启用成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); initDataView(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void tsbtnDisable_Click(object sender, EventArgs e) { try { if (this.dataGridView1.CurrentRow == null) return; var list = ((BindingSource)dataGridView1.DataSource).DataSource as List; int liIndex = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引 if (list[liIndex].Code=="admin") throw new Exception("管理员不可禁用!"); if (MessageBox.Show($"确认禁用?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { list[liIndex].State = false; //只更新指定列 if (!service.Update(it => new User() { State = list[liIndex].State }, it => it.Id == list[liIndex].Id)) throw new Exception("禁用失败!"); MessageBox.Show("禁用成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); initDataView(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void tsbtnResetPW_Click(object sender, EventArgs e) { if (this.dataGridView1.CurrentRow == null) return; var list = ((BindingSource)dataGridView1.DataSource).DataSource as List; int liIndex = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引 if (MessageBox.Show($"确认重置密码?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { string szUserPw = Utils.Util.GetMD5(""); list[liIndex].Password = szUserPw; //只更新指定列 if (!service.Update(it => new User() { Password = list[liIndex].Password }, it => it.Id == list[liIndex].Id)) throw new Exception("重置失败!"); MessageBox.Show("重置成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); initDataView(); } } private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { var list = ((BindingSource)dataGridView1.DataSource).DataSource as List; int liIndex = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引 FrmUserInfo frm = new FrmUserInfo(list[liIndex]); frm.ShowDialog(this); initDataView(); } private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { var list = ((BindingSource)dataGridView1.DataSource).DataSource as List; for (int i = 0; i < dataGridView1.Rows.Count; i++) { if (list[i].RoleInfo != null) dataGridView1.Rows[i].Cells["colRoleName"].Value = list[i].RoleInfo.Name; } } } }