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 GeBoShi.UI.User { public partial class RightMgrFrm : Form { RoleService _Service = new RoleService(); private List _RightList; User _FatherUser; public RightMgrFrm(User fatherUser) { InitializeComponent(); this.uiTitel1.ShowContrlBox(false, false, true); this.uiTitel1.FatherForm = this; dataGridView1.AutoGenerateColumns = false; dataGridView2.AutoGenerateColumns = false; _FatherUser = fatherUser; } private void InitDataView() { _RightList = _Service.GetRightItems(); dataGridView2.DataSource = new BindingSource(_RightList, null); var list = _Service.GetListNav(); //tsslCount.Text = $"共 {list.Count} 条记录"; dataGridView1.DataSource = new BindingSource(list, null); } private void RightMgrFrm_Load(object sender, EventArgs e) { InitDataView(); } private void tsbtnSave_Click(object sender, EventArgs e) { dataGridView2.CurrentCell = null;//更新当前编辑单元格 var list = ((BindingSource)dataGridView1.DataSource).DataSource as List; int liIndex = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引 foreach (var item in _RightList) { if (list[liIndex].RightList.FirstOrDefault(m => m.Id == item.Id) == null) { if (item.check) list[liIndex].RightList.Add(item); } else { if (!item.check) list[liIndex].RightList.Remove(item); } } if (_Service.UpdateNav(list[liIndex])) MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); else MessageBox.Show("保存失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } private void tsbtnAdd_Click(object sender, EventArgs e) { NewRoleFrm frm = new NewRoleFrm(_FatherUser); 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 (MessageBox.Show($"确认删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { if (!_Service.DelNav(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 tsbtnClose_Click(object sender, EventArgs e) { this.Close(); } private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { var list = ((BindingSource)dataGridView1.DataSource).DataSource as List; int liIndex = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引 NewRoleFrm frm = new NewRoleFrm(_FatherUser, list[liIndex]); if (frm.ShowDialog(this) == DialogResult.OK) InitDataView(); } private void dataGridView1_SelectionChanged(object sender, EventArgs e) { var list = ((BindingSource)dataGridView1.DataSource).DataSource as List; int liIndex = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引 foreach (var item in _RightList) item.check = list[liIndex].RightList.FirstOrDefault(m => m.Id == item.Id) != null; dataGridView2.Refresh(); } private void toolStrip1_Paint(object sender, PaintEventArgs e) { if ((sender as ToolStrip).RenderMode == ToolStripRenderMode.System) { Rectangle rect = new Rectangle(0, 0, this.toolStrip1.Width, this.toolStrip1.Height - 2); e.Graphics.SetClip(rect); } } } }