//----------------------------------------------------------------
// <copyright file="ExplorerForm.cs" >
// Copyright (c) Wenzy , All rights reserved.
// author:温正宇 E-Mail:wenzy@tom.com MyBlog:wenzy.cnblogs.com
// All rights reserved.
// </copyright>
//----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MemberShipDAL.DataObject;
using WMFConfig.Bussiness;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Document;
using MemberShipDAL.DataObject.DataResourcesTableAdapters;
namespace WMFConfig
{
/// <summary>
/// 权限配置工具
/// </summary>
public partial class ExplorerForm : Form
{
#region 变量定义
/// <summary>
/// 标记数据是否已经加载完毕
/// </summary>
public static bool privilegeInited = false;
/// <summary>
/// 开始被拖拽的节点
/// </summary>
private TreeNode dragNode = null;
/// <summary>
/// 临时的用于选择的节点
/// </summary>
private TreeNode tempDropNode = null;
/// <summary>
/// 计时器,用户滚动
/// </summary>
private Timer timer = new Timer();
/// <summary>
/// 计时器,节点展开
/// </summary>
private Timer timerExpand = new Timer();
#endregion
#region 系统事件
public ExplorerForm()
{
InitializeComponent();
timer.Interval = 200;
timer.Tick += new EventHandler(timer_Tick);
timerExpand.Interval = 1000;
timerExpand.Tick += new EventHandler(timerExpand_Tick);
}
private void ExplorerForm_Load(object sender, EventArgs e)
{
InitForm();
//以c#的方式高亮,初始化文本框
txtIcs_Content.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy("C#");
txtIcs_Content.Encoding = System.Text.Encoding.Default;
}
private void 退出xToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Dispose();
Application.Exit();
}
private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
{
using (AboutForm a = new AboutForm())
{
a.ShowDialog();
}
}
/// <summary>
/// 操作日志
/// </summary>
/// <param name="logText"></param>
/// <param name="showTime"></param>
private void SysLog(string logText, bool showTime)
{
if (logText.Trim() == string.Empty)
return;
if (showTime)
logText = "[" + DateTime.Now.ToString() + "]:" + logText;
toolStripStatusInfo.Text = logText;
logText = logText + "/n/n";
txt_OptLog.Text = logText + txt_OptLog.Text;
}
#endregion
#region TabControl事件
/// <summary>
/// 如果数据还没有保存,那就提示用户要保存数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tabControl1_Deselecting(object sender, TabControlCancelEventArgs e)
{
if (this.dataResources.HasChanges())
{
if (MessageBox.Show("您还没有保存数据,确定要离开[" + e.TabPage.Text + "]操作?", "保存数据", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
{
this.dataResources.RejectChanges();
}
else
{
e.Cancel = true;
}
}
}
//给这些数据进行重新绑定
private void tabControl1_Selected(object sender, TabControlEventArgs e)
{
try
{
switch (e.TabPage.Name)
{
case "tabPagePrivilege":
//绑定权限表
Privilegebind();
lbl_TableName.Text = "与[" + trv_Resources.SelectedNode.Text + "]权限组(ResourceGroup)相关的权限记录,共 " + wmfPrivilegeBindingSource.Count.ToString() + " 条";
break;
case "tabPageResManage":
this.dataResources.wmf_Resources.Clear();
this.wmf_ResourcesTableAdapter.Fill(this.dataResources.wmf_Resources);
lbl_TableName.Text = "与资源(Resource)相关的权限记录,共 " + wmfPrivilegeBindingSource.Count.ToString() + " 条";
break;
case "tabPageResGroup":
this.dataResources.wmf_ResourcesGroup.Clear();
this.wmf_ResourcesGroupTableAdapter.Fill(this.dataResources.wmf_ResourcesGroup);
lbl_TableName.Text = "与[" + trv_Resources.SelectedNode.Text + "]权限组(ResourceGroup)相关的权限记录,共 " + wmfPrivilegeBindingSource.Count.ToString() + " 条";
break;
case "tabOperation":
this.dataResources.wmf_Operation.Clear();
this.wmf_OperationTableAdapter.Fill(this.dataResources.wmf_Operation);
lbl_TableName.Text = "与操作(Operation)相关的权限记录,共 " + wmfPrivilegeBindingSource.Count.ToString() + " 条";
break;
default:
break;
}
DataSelectByTree();
this.dataResources.wmf_Privilege.Clear();
this.wmf_PrivilegeTableAdapter.Fill(this.dataResources.wmf_Privilege);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw;
}
}
#endregion
#region 系统初始化,数据绑定
/// <summary>
/// 数据初始化
/// </summary>
private void InitForm()
{
this.dataResources.wmf_Resources.Clear();
this.dataResources.wmf_ResourcesGroup.Clear();
this.dataResources.wmf_Operation.Clear();
this.dataResources.wmf_Privilege.Clear();
// TODO: 这行代码将数据加载到表“dataResources.wmf_Privilege”中。
this.wmf_PrivilegeTableAdapter.Fill(this.dataResources.wmf_Privilege);
// TODO: 这行代码将数据加载到表“dataResources.wmf_Operation”中。
this.wmf_OperationTableAdapter.Fill(this.dataResources.wmf_Operation);
// TODO: 这行代码将数据加载到表“dataResources.wmf_ResourcesGroup”中。
this.wmf_ResourcesGroupTableAdapter.Fill(this.dataResources.wmf_ResourcesGroup);
// TODO: 这行代码将数据加载到表“dataResources.wmf_Resources”中。
this.wmf_ResourcesTableAdapter.Fill(this.dataResources.wmf_Resources);
//判断是否有数据,如果没有,提示并退出
if (this.dataResources.wmf_ResourcesGroup.Rows.Count == 0)
{
this.Hide();
MessageBox.Show("资源组的根节点未建立,请先运行在Web项目下的App_Data下的BaseData.sql,建立起基本的数据, 再打开本系统", "RMF:基本数据尚未建立", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Dispose();
//退出程序
Application.Exit();
}
//绑定资源树
trv_Resources.Nodes.Clear();
new ResourcesBLL().ResourcesTreeBind("", null, dataResources.wmf_ResourcesGroup, trv_Resources);
trv_Resources.ExpandAll();
//绑定权限表
Privilegebind();
}
/// <summary>
/// 选择树以后,重新绑定列表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void trv_Resources_AfterSelect(object sender, TreeViewEventArgs e)
{
DataSelectByTree();
Privilegebind();
}
//绑定权限表
private void Privilegebind()
{
privilegeInited = false;
/*this.dataResources.wmf_Resources.Clear();
this.dataResources.wmf_ResourcesGroup.Clear();
this.dataResources.wmf_Operation.Clear();
this.dataResources.wmf_Privilege.Clear();
// TODO: 这行代码将数据加载到表“dataResources.wmf_Privilege”中。
this.wmf_PrivilegeTableAdapter.Fill(this.dataResources.wmf_Privilege);
// TODO: 这行代码将数据加载到表“dataResources.wmf_Operation”中。
this.wmf_OperationTableAdapter.Fill(this.dataResources.wmf_Operation);
// TODO: 这行代码将数据加载到表“dataResources.wmf_ResourcesGroup”中。
this.wmf_ResourcesGroupTableAdapter.Fill(this.dataResources.wmf_ResourcesGroup);
// TODO: 这行代码将数据加载到表“dataResources.wmf_Resources”中。
this.wmf_ResourcesTableAdapter.Fill(this.dataResources.wmf_Resources);*/
new PrivilegeBLL().PrivilegeBind(trv_Resources.SelectedNode.Name, this.dataResources.wmf_Operation, this.dataResources.wmf_Resources, this.dataResources.wmf_Privilege, dg_Privilege);
privilegeInited = true;
}
/// <summary>
/// 根据树选择数据内容
/// </summary>
private void DataSelectByTree()
{
try
{
this.dataResources.wmf_Privilege.Clear();
// TODO: 这行代码将数据加载到表“dataResources.wmf_Privilege”中。
this.wmf_PrivilegeTableAdapter.Fill(this.dataResources.wmf_Privilege);
switch (tabControl1.SelectedTab.Name)
{
case "tabPageResGroup":
wmfResourcesBindingSource.Filter = string.Empty;
wmfResourcesGroupBindingSource.Filter = "[ParentId]='" + trv_Resources.SelectedNode.Name + "'";
break;
case "tabPagePrivilege":
break;
case "tabPageResManage":
wmfResourcesGroupBindingSource.Filter = string.Empty;
wmfResourcesBindingSource.Filter = "[ResourceGroupId]='" + trv_Resources.SelectedNode.Name + "'";
break;
case "tabOperation":
break;
default:
break;
}
//过滤Privilege,包括tabPageResGroup(权限组管理)都要过滤
if (toolStripDisplayPrivilege.Checked && ((tabControl1.SelectedTab.Name == "tabPageResGroup") || (tabControl1.SelectedTab.Name == "tabPagePrivilege")))
{
StringBuilder privilegeFilter = new StringBuilder("Resourcesid = '");
foreach (DataResources.wmf_ResourcesRow resRow in this.dataResources.wmf_Resources.Select("ResourceGroupId='" + trv_Resources.SelectedNode.Name + "'"))
{
privilegeFilter.Append(resRow.ResourcesId + "' or Resourcesid = '");
}
int lastOr = privilegeFilter.ToString().LastIndexOf("or");
if (lastOr > 0)
{
privilegeFilter.Remove(lastOr, privilegeFilter.Length - lastOr);
}
else
{
privilegeFilter.Remove(0, privilegeFilter.Length);
}
this.wmfPrivilegeBindingSource.Filter = privilegeFilter.ToString();
lbl_TableName.Text = "与[" + trv_Resources.SelectedNode.Text + "]权限组(ResourceGroup)相关的权限记录,共 " + wmfPrivilegeBindingSource.Count.ToString() + " 条";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw;
}
}
#endregion
#region 工具以及菜单栏按钮事件
private void btn_ResReload_Click(object sender, EventArgs e)
{
//重新初始化
InitForm();
}
private void 重新载入RToolStripMenuItem_Click(object sender, EventArgs e)
{
//重新初始化
InitForm();
}
private void btn_Close_MouseEnter(object sender, EventArgs e)
{
btn_Close.FlatStyle = FlatStyle.Popup;
}
private void btn_Close_MouseLeave(object sender, EventArgs e)
{
btn_Close.FlatStyle = FlatStyle.Flat;
}
private void btn_Close_Click(object sender, EventArgs e)
{
panel1.Visible = false;
toolStripDisplayTree.Checked = false;
}
private void toolStripDisplayTree_CheckedChanged(object sender, EventArgs e)
{
panel1.Visible = toolStripDisplayTree.Checked;
}
private void toolStripLog_CheckedChanged(object sender, EventArgs e)
{
txt_OptLog.Visible = toolStripLog.Checked;
splitterLog.Visible = toolStripLog.Checked;
}
private void toolStripDisplayPrivilege_CheckedChanged(object sender, EventArgs e)
{
panel2.Visible = toolStripDisplayPrivilege.Checked;
splitterPrivilege.Visible = toolStripDisplayPrivilege.Checked;
}
private void 权限组ToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripDisplayTree.Checked = 权限组ToolStripMenuItem.Checked;
}
private void 日志ToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripLog.Checked = 日志ToolStripMenuItem.Checked;
}
private void 权限ToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripDisplayPrivilege.Checked = 权限ToolStripMenuItem.Checked;
}
private void 保存当前文件SToolStripMenuItem_Click_1(object sender, EventArgs e)
{
btn_ResSave_Click(sender, e);
}
/// <summary>
/// 让关闭按钮随着Splitter移动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void splitter1_SplitterMoved(object sender, SplitterEventArgs e)
{
btn_Close.Location = new Point(e.X - 25, btn_Close.Location.Y);
}
private void 资源操作选择SToolStripMenuItem_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabControl1.TabPages["tabPagePrivilege"];
}
private void 资源管理RToolStripMenuItem_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabControl1.TabPages["tabPageResManage"];
}
private void 权限组编辑GToolStripMenuItem_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabControl1.TabPages["tabPageResGroup"];
}
private void 操作类型管理OToolStripMenuItem_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabControl1.TabPages["tabOperation"];
}
private void 枚举代码生成EToolStripMenuItem_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabControl1.TabPages["tabPageCodeGen"];
}
#endregion
#region Guid相关功能
private void toolStripBtn_GenGuid_Click(object sender, EventArgs e)
{
//生成Guid
toolStripTxt_Guid.Text = Guid.NewGuid().ToString();
}
private void toolStripTxt_Guid_DoubleClick(object sender, EventArgs e)
{
toolStripTxt_Guid.SelectAll();
}
#endregion
#region 保存数据
/// <summary>
/// 保存按钮,按照当前显示的内容不同,保存不同的数据。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_ResSave_Click(object sender, EventArgs e)
{
try
{
switch (tabControl1.SelectedTab.Name)
{
case "tabPageResManage":
this.wmf_ResourcesTableAdapter.Update(this.dataResources.wmf_Resources);
MessageBox.Show("资源[Resources]更新成功", tabControl1.SelectedTab.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case "tabPageResGroup":
this.wmf_ResourcesGroupTableAdapter.Update(this.dataResources.wmf_ResourcesGroup);
MessageBox.Show("权限组[ResourcesGroup]更新成功", tabControl1.SelectedTab.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case "tabOperation":
this.wmf_OperationTableAdapter.Update(this.dataResources.wmf_Operation);
MessageBox.Show("操作类型[Operation]更新成功", tabControl1.SelectedTab.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case "tabPageCodeGen":
SaveFile();
break;
default:
MessageBox.Show("当前的数据为自动保存", "系统提示:", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
break;
}
InitForm();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Privilege中checkbox发生改变后保存相关数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dg_Privilege_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (!privilegeInited)
{
return;
}
string[] optTagValue = dg_Privilege.Columns[e.ColumnIndex].HeaderCell.Tag.ToString().Split('|');
string[] resTagValue = dg_Privilege.Rows[e.RowIndex].HeaderCell.Tag.ToString().Split('|');
int operationId = Convert.ToInt32(optTagValue[0]);
Guid resourcesId = new Guid(resTagValue[0]);
string operationValue = dg_Privilege.Columns[e.ColumnIndex].HeaderCell.Value.ToString();
string resourcesValue = dg_Privilege.Rows[e.RowIndex].HeaderCell.Value.ToString();
//如果值为假,就删除在数据库里面相应的记录
if (Convert.ToBoolean(dg_Privilege[e.ColumnIndex, e.RowIndex].Value))
{
SysLog("增加记录:" + operationValue + "+" + resourcesValue, true);
this.wmf_PrivilegeTableAdapter.Insert(Guid.NewGuid(), operationId, resourcesId, resTagValue[1] + optTagValue[1], resourcesValue + operationValue, null, null, 0);
}
else
{
//要删除以前,要把PrivilegeInRoles里面的东西一并删除
new PrivilegeInRolesBLL().DeletePrivilegeInRoles(operationId, resourcesId);
this.wmf_PrivilegeTableAdapter.DeleteQuery(operationId, resourcesId);
SysLog("删除权限记录:" + operationValue + "+" + resourcesValue, true);
}
}
#endregion
#region ContextMenu事件
private void 新建组AToolStripMenuItem_Click(object sender, EventArgs e)
{
AddNewGroup();
}
private void 重命名RToolStripMenuItem_Click(object sender, EventArgs e)
{
trv_Resources.SelectedNode.BeginEdit();
}
private void 删除DToolStripMenuItem_Click(object sender, EventArgs e)
{
//不能删除根节点,否则会出错
if (trv_Resources.SelectedNode.Text == "所有资源")
{
MessageBox.Show("不能删除根节点", "删除权限组", MessageBoxButtons.OK, MessageBoxIcon.Stop);
return;
}
if (MessageBox.Show("是否删除权限组[" + trv_Resources.SelectedNode.Text + "]以及下属权限", "删除权限组提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
DeleteResourceGroup(trv_Resources.SelectedNode);
}
}
private void 全部选择ToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (DataGridViewCell cell in dg_Privilege.SelectedCells)
{
cell.Value = true;
}
}
private void 全部取消ToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (DataGridViewCell cell in dg_Privilege.SelectedCells)
{
cell.Value = false;
}
}
private void 反向选择ToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (DataGridViewCell cell in dg_Privilege.SelectedCells)
{
cell.Value = !Convert.ToBoolean(cell.Value);
}
}
private void ts_Add_Click(object sender, EventArgs e)
{
AddNewGroup();
}
private void 修改名字ToolStripMenuItem_Click(object sender, EventArgs e)
{
trv_Resources.SelectedNode.BeginEdit();
}
#endregion
#region 代码生成
private void btn_SaveFile_Click(object sender, EventArgs e)
{
SaveFile();
}
private void btn_Resource_Click(object sender, EventArgs e)
{
txtIcs_Content.Text = CodeGen.ResourceCode();
lbl_FileName.Text = "Privilege.cs";
}
public void SaveFile()
{
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.Filter = "C# 文件(*.cs)|*.cs";
saveFile.Title = ("保存WMFConfig工具自动生成的代码文件");
saveFile.FileName = lbl_FileName.Text;
saveFile.ShowDialog();
if (saveFile.FileName != "")
{
string fileName = saveFile.FileName;
txtIcs_Content.SaveFile(fileName);
}
}
private void 保存当前文件SToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFile();
}
#endregion
#region TreeVie相关操作
/// <summary>
/// 编辑节点
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void trv_Resources_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
if (e.Label == null)
{
//如果是新节点
if (e.Node.Tag != null)
{
e.Node.Remove();
}
return;
}
if (e.Label != null && e.Label.Trim() == string.Empty && e.Label != e.Node.Text)
{
e.CancelEdit = true;
}
else
{
//如果是新节点
if (e.Node.Tag != null)
{
wmf_ResourcesGroupTableAdapter.Insert(new Guid(e.Node.Name), e.Label.Trim(), new Guid(e.Node.Parent.Name), "", e.Node.Level);
SysLog("添加[" + e.Label + "]节点成功!!", true);
e.Node.Tag = null;
//trv_Resources.SelectedNode = e.Node;
}
else
{
//更新到数据库
if (wmf_ResourcesGroupTableAdapter.UpdateGroupName(e.Label, e.Node.Text) == 1)
{
SysLog("名字[" + e.Node.Text + "]更新为[" + e.Label + "]成功!!", true);
toolStripStatus.Text = DateTime.Now.ToString() + " 更新权限组节点名称成功";
}
else
{
toolStripStatus.Text = "警告:更新权限组节点名称失败!!";
}
}
}
}
/// <summary>
/// 键盘操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void trv_Resources_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
//编辑节点
if (e.KeyCode == Keys.F2)
{
trv_Resources.SelectedNode.BeginEdit();
}
//编辑节点
if (e.KeyCode == Keys.F3)
{
AddNewGroup();
}
//删除节点
if (e.KeyCode == Keys.Delete)
{
if (MessageBox.Show("是否删除权限组[" + trv_Resources.SelectedNode.Text + "]以及下属权限", "删除权限组提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
DeleteResourceGroup(trv_Resources.SelectedNode);
}
}
}
/// <summary>
/// 删除已选择的权限组
/// </summary>
private void DeleteResourceGroup(TreeNode selectNode)
{
try
{
///遍历,把子节点全部删除
foreach (TreeNode child in selectNode.Nodes)
{
DeleteResourceGroup(child);
}
//删除Resources
DataResources.wmf_ResourcesRow[] resourceRows = (DataResources.wmf_ResourcesRow[])this.dataResources.wmf_Resources.Select("[ResourceGroupId]='" + selectNode.Name + "'");
foreach (MemberShipDAL.DataObject.DataResources.wmf_ResourcesRow resourceRow in resourceRows)
{
DataResources.wmf_PrivilegeRow[] privilegeRows = (DataResources.wmf_PrivilegeRow[])this.dataResources.wmf_Privilege.Select("[ResourcesId]='" + resourceRow.ResourcesId + "'");
foreach (DataResources.wmf_PrivilegeRow privilegeRow in privilegeRows)
{
//删除PrivilegeInRoles
new PrivilegeInRolesBLL().DeletePrivilegeInRoles(privilegeRow.PrivilegeId);
}
//删除Privilege
wmf_PrivilegeTableAdapter.DeleteByResourceId(resourceRow.ResourcesId);
}
wmf_ResourcesTableAdapter.DeleteByGroupId(new Guid(selectNode.Name));
wmf_ResourcesGroupTableAdapter.DeleteByGroupId(new Guid(selectNode.Name));
selectNode.Remove();
SysLog("删除节点[" + selectNode.Name + "]成功", true);
}
catch (Exception)
{
toolStripStatus.Text = "警告:删除失败!!";
throw;
}
}
private void trv_Resources_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
trv_Resources.SelectedNode = e.Node;
}
}
public void AddNewGroup()
{
TreeNode newNode = new TreeNode("新建权限组");
newNode.Name = Guid.NewGuid().ToString();
newNode.Tag = "NewGroup";
trv_Resources.SelectedNode.Nodes.Add(newNode);
trv_Resources.SelectedNode.Expand();
newNode.BeginEdit();
}
#endregion
#region TreeView拖拽功能
private void trv_Resources_ItemDrag(object sender, ItemDragEventArgs e)
{
// 获得选择的节点
this.dragNode = (TreeNode)e.Item;
this.trv_Resources.SelectedNode = this.dragNode;
// 为了拖拽得图像,重置 image list
this.imageListDrag.Images.Clear();
this.imageListDrag.ImageSize = new Size(this.dragNode.Bounds.Size.Width + this.trv_Resources.Indent, this.dragNode.Bounds.Height);
// 创建一个新的图像
//这个图像包含了要拖拽的节点
Bitmap bmp = new Bitmap(this.dragNode.Bounds.Width + this.trv_Resources.Indent, this.dragNode.Bounds.Height);
// 从bitmap获取Graphics
Graphics gfx = Graphics.FromImage(bmp);
// 绘制节点图标到bitmap
gfx.DrawImage(this.imgFolder.Images[0], 0, 0);
// 绘制节点名称到图片
gfx.DrawString(this.dragNode.Text,
this.trv_Resources.Font,
new SolidBrush(this.trv_Resources.ForeColor),
(float)this.trv_Resources.Indent, 1.0f);
//把图片加入ImageList
this.imageListDrag.Images.Add(bmp);
// 获得客户端鼠标节点
Point p = this.trv_Resources.PointToClient(Control.MousePosition);
//计算鼠标和节点边缘的距离
int dx = p.X + this.trv_Resources.Indent - this.dragNode.Bounds.Left - 15;
int dy = p.Y - this.dragNode.Bounds.Top + 48;
// 开始拖拽图片
if (DragHelper.ImageList_BeginDrag(this.imageListDrag.Handle, 0, dx, dy))
{
// 开始拖拽
this.trv_Resources.DoDragDrop(bmp, DragDropEffects.Move);
// 停止拖拽
DragHelper.ImageList_EndDrag();
}
}
private void trv_Resources_DragDrop(object sender, DragEventArgs e)
{
DragHelper.ImageList_DragLeave(this.trv_Resources.Handle);
//获得拖拽的节点
TreeNode dropNode = this.trv_Resources.GetNodeAt(this.trv_Resources.PointToClient(new Point(e.X, e.Y)));
// 如果目标节点不等于本身,就添加到其子节点
if (this.dragNode != dropNode)
{
if (dropNode.Name != dragNode.Parent.Name)
{
//在此把数据库更新
if (this.wmf_ResourcesGroupTableAdapter.UpdateParentId(new Guid(dropNode.Name), new Guid(dragNode.Name)) == 1)
{
//提示成功
SysLog("移动[" + dragNode.Text + "]到[" + dropNode.Text + "]成功!!", true);
toolStripStatus.Text = DateTime.Now.ToString() + "节点移动成功!!";
}
else
{
toolStripStatus.Text = DateTime.Now.ToString() + "节点移动失败";
}
}
else
{
MessageBox.Show("无法移动[" + dragNode.Text + "],目标节点和源节点相同", "移动节点时出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// 取出拖拽节点的父节点
if (this.dragNode.Parent == null)
{
this.trv_Resources.Nodes.Remove(this.dragNode);
}
else
{
this.dragNode.Parent.Nodes.Remove(this.dragNode);
}
// 添加节点到目标节点
dropNode.Nodes.Add(this.dragNode);
dropNode.Expand();
// 把拖拽的节点设为空
this.dragNode = null;
//取消滚动的计时器
this.timerExpand.Enabled = false;
this.timer.Enabled = false;
}
}
private void trv_Resources_DragEnter(object sender, DragEventArgs e)
{
DragHelper.ImageList_DragEnter(this.trv_Resources.Handle, e.X - this.trv_Resources.Left, e.Y - this.trv_Resources.Top);
// 滚动
this.timer.Enabled = true;
this.timerExpand.Enabled = true;
}
private void trv_Resources_DragLeave(object sender, EventArgs e)
{
DragHelper.ImageList_DragLeave(this.trv_Resources.Handle);
// 停止滚动
this.timer.Enabled = false;
this.timerExpand.Enabled = false;
}
private void trv_Resources_DragOver(object sender, DragEventArgs e)
{
//计算位置并且移动图片
Point formP = this.PointToClient(new Point(e.X, e.Y));
DragHelper.ImageList_DragMove(formP.X - this.trv_Resources.Left, formP.Y - this.trv_Resources.Top);
// 获得真正的移动节点
TreeNode dropNode = this.trv_Resources.GetNodeAt(this.trv_Resources.PointToClient(new Point(e.X, e.Y)));
if (dropNode == null)
{
e.Effect = DragDropEffects.None;
return;
}
e.Effect = DragDropEffects.Move;
// 如果鼠标在新的节点上,那就选中
if (this.tempDropNode != dropNode)
{
DragHelper.ImageList_DragShowNolock(false);
this.trv_Resources.SelectedNode = dropNode;
DragHelper.ImageList_DragShowNolock(true);
tempDropNode = dropNode;
}
//避免移动到自己的子节点上面
TreeNode tmpNode = dropNode;
while (tmpNode.Parent != null)
{
if (tmpNode.Parent == this.dragNode)
e.Effect = DragDropEffects.None;
tmpNode = tmpNode.Parent;
}
}
private void trv_Resources_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
if (e.Effect == DragDropEffects.Move)
{
// 在拖拽的时候,显示鼠标图标
e.UseDefaultCursors = false;
this.trv_Resources.Cursor = Cursors.Default;
}
else e.UseDefaultCursors = true;
}
private void timer_Tick(object sender, EventArgs e)
{
// 获得鼠标所在位置的节点
Point pt = PointToClient(Control.MousePosition);
TreeNode node = this.trv_Resources.GetNodeAt(pt);
if (node == null) return;
// 如果鼠标靠近上面,就向上滚动
if (pt.Y < 30)
{
// 选择上面一个节点
if (trv_Resources.TopNode.PrevNode != null)
{
node = trv_Resources.TopNode.PrevNode;
// 隐藏拖拽的图片
DragHelper.ImageList_DragShowNolock(false);
// 拖拽并且刷新
node.EnsureVisible();
this.trv_Resources.Refresh();
// 显示图片
DragHelper.ImageList_DragShowNolock(true);
}
}
// 如果鼠标靠近下面,就向下滚动
else if (pt.Y > this.trv_Resources.Size.Height - 30)
{
if (node.NextVisibleNode != null)
{
node = node.NextVisibleNode;
DragHelper.ImageList_DragShowNolock(false);
node.EnsureVisible();
this.trv_Resources.Refresh();
DragHelper.ImageList_DragShowNolock(true);
}
}
}
void timerExpand_Tick(object sender, EventArgs e)
{
if (trv_Resources.SelectedNode != null)
trv_Resources.SelectedNode.Expand();
}
#endregion
#region DataGridView错误提示
private void dg_Resources_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
MessageBox.Show("您插入的数据有误,详细信息如下:/n" + e.Exception.Message, "插入数据错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void dg_Operate_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
MessageBox.Show("您插入的数据有误,详细信息如下:/n" + e.Exception.Message, "插入数据错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
#endregion
#region Resource资源组的DataGridView事件
/// <summary>
/// 自动把中文名复职到英文名单元格内
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dg_Resources_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
dg_Resources_CellEndEdit(sender, e);
}
private void dg_Resources_CellLeave(object sender, DataGridViewCellEventArgs e)
{
dg_Resources_CellEndEdit(sender, e);
}
/// <summary>
/// 单元格快捷键
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dg_Resources_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
//Ctrl+S,保存当前记录
if (e.KeyData == (Keys.Control | Keys.S))
btn_ResSave_Click(sender, e);
}
private void dg_Resources_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
string columnCNName = "resourcesCNNameDataGridViewTextBoxColumn";
string columnName = "resourcesNameDataGridViewTextBoxColumn";
string copyValue;
if ((e.RowIndex > -1) && (e.ColumnIndex > -1) && (dg_Resources.Rows[e.RowIndex].Cells[columnCNName].Value != null))
{
copyValue = dg_Resources.Rows[e.RowIndex].Cells[columnCNName].Value.ToString().Trim();
if ((dg_Resources.Columns[e.ColumnIndex].Name == columnCNName) && (dg_Resources.Rows[e.RowIndex].Cells[columnName].Value.ToString() == string.Empty))
{
dg_Resources.Rows[e.RowIndex].Cells[columnName].Value = copyValue;
}
}
}
/// <summary>
/// 用户删除行的时候,要删除与其相关的记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dg_Resources_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
if ((!e.Row.IsNewRow) && (this.wmfPrivilegeBindingSource.Count != 0))
{
if (MessageBox.Show("请确认是否删除与[" + e.Row.Cells["resourcesCNNameDataGridViewTextBoxColumn"].Value.ToString() + "]相关的记录?", "删除相关记录", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
DeletePrivilegeByResourceId(new Guid(e.Row.Cells["resourcesIdDataGridViewTextBoxColumn"].Value.ToString()));
}
else
{
e.Cancel = true;
}
}
}
/// <summary>
/// 用户编辑行的时候,删除相关记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dg_Resources_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
this.wmfPrivilegeBindingSource.Filter = "[ResourcesId]='" + dg_Resources.Rows[e.RowIndex].Cells["resourcesIdDataGridViewTextBoxColumn"].Value.ToString() + "'";
if ((!dg_Resources.Rows[e.RowIndex].IsNewRow) && (this.wmfPrivilegeBindingSource.Count != 0) && dg_Resources.Columns[e.ColumnIndex].Name != "descriptionDataGridViewTextBoxColumn" && dg_Resources.Columns[e.ColumnIndex].Name != "ResourcesOrder" && dg_Resources.Columns[e.ColumnIndex].Name != "ResourceGroupId")
{
if (MessageBox.Show("在编辑关键信息以前,请确认是否删除与[" + dg_Resources["resourcesCNNameDataGridViewTextBoxColumn", e.RowIndex].Value.ToString() + "]相关的记录?", "编辑前删除相关记录", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
DeletePrivilegeByResourceId(new Guid(dg_Resources["resourcesIdDataGridViewTextBoxColumn", e.RowIndex].Value.ToString()));
}
else
{
e.Cancel = true;
}
}
}
/// <summary>
/// 给列表添加自动生成GUID的功能
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dg_Resources_RowEnter(object sender, DataGridViewCellEventArgs e)
{
if (dg_Resources.Rows[e.RowIndex].IsNewRow)
{
dg_Resources.Rows[e.RowIndex].Cells["resourcesIdDataGridViewTextBoxColumn"].Value = Guid.NewGuid().ToString();
dg_Resources.Rows[e.RowIndex].Cells["ResourceGroupId"].Value = trv_Resources.SelectedNode.Name;
dg_Resources.Rows[e.RowIndex].Cells["ResourcesOrder"].Value = this.dataResources.wmf_Resources.Rows.Count + 1;
}
if (dg_Resources.Rows[e.RowIndex].Cells["resourcesIdDataGridViewTextBoxColumn"].Value != null)
{
// 过滤
this.wmfPrivilegeBindingSource.Filter = "[ResourcesId]='" + dg_Resources.Rows[e.RowIndex].Cells["resourcesIdDataGridViewTextBoxColumn"].Value.ToString() + "'";
lbl_TableName.Text = "与资源[" + dg_Resources.Rows[e.RowIndex].Cells["resourcesCNNameDataGridViewTextBoxColumn"].Value.ToString() + "]相关的权限记录,共 " + wmfPrivilegeBindingSource.Count.ToString() + " 条记录";
}
}
/// <summary>
/// 根据资源删除相关的权限
/// </summary>
/// <param name="operationId"></param>
private void DeletePrivilegeByResourceId(Guid resourceID)
{
//检验这个权限是否被使用,如果有被使用,则删除
foreach (DataResources.wmf_PrivilegeRow row in wmf_PrivilegeTableAdapter.GetDataByResourceId(resourceID))
{
//删除PrivilegeInRoles
new PrivilegeInRolesBLL().DeletePrivilegeInRoles(row.ResourcesId);
}
int i = wmf_PrivilegeTableAdapter.DeleteByResourceId(resourceID);
//重新加载数据
this.dataResources.wmf_Privilege.Clear();
wmf_PrivilegeTableAdapter.Fill(dataResources.wmf_Privilege);
SysLog("成功删除了" + i.ToString() + "条与resourceID[" + resourceID + "]相关的记录", true);
}
#endregion
#region Operate操作类型的DataGridView事件
private void dg_Operate_RowEnter(object sender, DataGridViewCellEventArgs e)
{
if (dg_Operate["operationIdDataGridViewTextBoxColumn", e.RowIndex].Value != null)
{
// 过滤
this.wmfPrivilegeBindingSource.Filter = "[OperationID]=" + dg_Operate["operationIdDataGridViewTextBoxColumn", e.RowIndex].Value.ToString();
lbl_TableName.Text = "与[" + dg_Operate["OperationCNName", e.RowIndex].Value.ToString() + "]操作(Operation)相关的权限记录共 " + wmfPrivilegeBindingSource.Count.ToString() + " 条记录";
}
}
private void dg_Operate_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
if ((!dg_Operate.Rows[e.RowIndex].IsNewRow) && (this.wmfPrivilegeBindingSource.Count != 0) && dg_Operate.Columns[e.ColumnIndex].Name != "descriptionDataGridViewTextBoxColumn2")
{
if (MessageBox.Show("在编辑关键信息以前,请确认是否删除与[" + dg_Operate["OperationCNName", e.RowIndex].Value.ToString() + "]相关的记录?", "编辑前删除相关记录", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
DeletePrivilegeByOperationId((int)dg_Operate["operationIdDataGridViewTextBoxColumn", e.RowIndex].Value);
}
else
{
e.Cancel = true;
}
}
}
/// <summary>
/// 根据操作类型删除相关的权限
/// </summary>
/// <param name="operationId"></param>
private void DeletePrivilegeByOperationId(int operationId)
{
//检验这个权限是否被使用,如果有被使用,则删除
foreach (DataResources.wmf_PrivilegeRow row in wmf_PrivilegeTableAdapter.GetDataByOperationID(operationId))
{
//删除PrivilegeInRoles
new PrivilegeInRolesBLL().DeletePrivilegeInRoles(row.PrivilegeId);
}
int i = wmf_PrivilegeTableAdapter.DeleteByOperationId(operationId);
//重新加载数据
this.dataResources.wmf_Privilege.Clear();
wmf_PrivilegeTableAdapter.Fill(dataResources.wmf_Privilege);
SysLog("成功删除了" + i.ToString() + "条与OperationID[" + operationId + "]相关的记录", true);
}
/// <summary>
/// 删除行,要删除相关记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dg_Operate_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
if ((!e.Row.IsNewRow) && (this.wmfPrivilegeBindingSource.Count != 0))
{
if (MessageBox.Show("在删除操作之前,请确认是否删除与此相关的信息", "删除相关记录", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
DeletePrivilegeByOperationId((int)e.Row.Cells["operationIdDataGridViewTextBoxColumn"].Value);
}
else
{
e.Cancel = true;
}
}
}
#endregion
#region 清理冗余数据
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripClean_Click(object sender, EventArgs e)
{
try
{
SysLog("开始清理在Privilege表中重复的数据", true);
int i = this.wmf_PrivilegeTableAdapter.DeleteRepeatData();
SysLog("清理在Privilege表中重复的数据成功!", true);
MessageBox.Show("清理冗余数据成功!", "清理冗余数据", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
SysLog("清理冗余数据数据失败,原因是:" + ex.Message, true);
MessageBox.Show("清理冗余数据失败,原因是:" + ex.Message, "清理冗余数据", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
#endregion
}
}
转自:http://www.koders.com/csharp/fid0EF67F8DA066B9DCC4804ED3E30AC8EFD8E93D85.aspx#L1032
// <copyright file="ExplorerForm.cs" >
// Copyright (c) Wenzy , All rights reserved.
// author:温正宇 E-Mail:wenzy@tom.com MyBlog:wenzy.cnblogs.com
// All rights reserved.
// </copyright>
//----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MemberShipDAL.DataObject;
using WMFConfig.Bussiness;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Document;
using MemberShipDAL.DataObject.DataResourcesTableAdapters;
namespace WMFConfig
{
/// <summary>
/// 权限配置工具
/// </summary>
public partial class ExplorerForm : Form
{
#region 变量定义
/// <summary>
/// 标记数据是否已经加载完毕
/// </summary>
public static bool privilegeInited = false;
/// <summary>
/// 开始被拖拽的节点
/// </summary>
private TreeNode dragNode = null;
/// <summary>
/// 临时的用于选择的节点
/// </summary>
private TreeNode tempDropNode = null;
/// <summary>
/// 计时器,用户滚动
/// </summary>
private Timer timer = new Timer();
/// <summary>
/// 计时器,节点展开
/// </summary>
private Timer timerExpand = new Timer();
#endregion
#region 系统事件
public ExplorerForm()
{
InitializeComponent();
timer.Interval = 200;
timer.Tick += new EventHandler(timer_Tick);
timerExpand.Interval = 1000;
timerExpand.Tick += new EventHandler(timerExpand_Tick);
}
private void ExplorerForm_Load(object sender, EventArgs e)
{
InitForm();
//以c#的方式高亮,初始化文本框
txtIcs_Content.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy("C#");
txtIcs_Content.Encoding = System.Text.Encoding.Default;
}
private void 退出xToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Dispose();
Application.Exit();
}
private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
{
using (AboutForm a = new AboutForm())
{
a.ShowDialog();
}
}
/// <summary>
/// 操作日志
/// </summary>
/// <param name="logText"></param>
/// <param name="showTime"></param>
private void SysLog(string logText, bool showTime)
{
if (logText.Trim() == string.Empty)
return;
if (showTime)
logText = "[" + DateTime.Now.ToString() + "]:" + logText;
toolStripStatusInfo.Text = logText;
logText = logText + "/n/n";
txt_OptLog.Text = logText + txt_OptLog.Text;
}
#endregion
#region TabControl事件
/// <summary>
/// 如果数据还没有保存,那就提示用户要保存数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tabControl1_Deselecting(object sender, TabControlCancelEventArgs e)
{
if (this.dataResources.HasChanges())
{
if (MessageBox.Show("您还没有保存数据,确定要离开[" + e.TabPage.Text + "]操作?", "保存数据", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
{
this.dataResources.RejectChanges();
}
else
{
e.Cancel = true;
}
}
}
//给这些数据进行重新绑定
private void tabControl1_Selected(object sender, TabControlEventArgs e)
{
try
{
switch (e.TabPage.Name)
{
case "tabPagePrivilege":
//绑定权限表
Privilegebind();
lbl_TableName.Text = "与[" + trv_Resources.SelectedNode.Text + "]权限组(ResourceGroup)相关的权限记录,共 " + wmfPrivilegeBindingSource.Count.ToString() + " 条";
break;
case "tabPageResManage":
this.dataResources.wmf_Resources.Clear();
this.wmf_ResourcesTableAdapter.Fill(this.dataResources.wmf_Resources);
lbl_TableName.Text = "与资源(Resource)相关的权限记录,共 " + wmfPrivilegeBindingSource.Count.ToString() + " 条";
break;
case "tabPageResGroup":
this.dataResources.wmf_ResourcesGroup.Clear();
this.wmf_ResourcesGroupTableAdapter.Fill(this.dataResources.wmf_ResourcesGroup);
lbl_TableName.Text = "与[" + trv_Resources.SelectedNode.Text + "]权限组(ResourceGroup)相关的权限记录,共 " + wmfPrivilegeBindingSource.Count.ToString() + " 条";
break;
case "tabOperation":
this.dataResources.wmf_Operation.Clear();
this.wmf_OperationTableAdapter.Fill(this.dataResources.wmf_Operation);
lbl_TableName.Text = "与操作(Operation)相关的权限记录,共 " + wmfPrivilegeBindingSource.Count.ToString() + " 条";
break;
default:
break;
}
DataSelectByTree();
this.dataResources.wmf_Privilege.Clear();
this.wmf_PrivilegeTableAdapter.Fill(this.dataResources.wmf_Privilege);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw;
}
}
#endregion
#region 系统初始化,数据绑定
/// <summary>
/// 数据初始化
/// </summary>
private void InitForm()
{
this.dataResources.wmf_Resources.Clear();
this.dataResources.wmf_ResourcesGroup.Clear();
this.dataResources.wmf_Operation.Clear();
this.dataResources.wmf_Privilege.Clear();
// TODO: 这行代码将数据加载到表“dataResources.wmf_Privilege”中。
this.wmf_PrivilegeTableAdapter.Fill(this.dataResources.wmf_Privilege);
// TODO: 这行代码将数据加载到表“dataResources.wmf_Operation”中。
this.wmf_OperationTableAdapter.Fill(this.dataResources.wmf_Operation);
// TODO: 这行代码将数据加载到表“dataResources.wmf_ResourcesGroup”中。
this.wmf_ResourcesGroupTableAdapter.Fill(this.dataResources.wmf_ResourcesGroup);
// TODO: 这行代码将数据加载到表“dataResources.wmf_Resources”中。
this.wmf_ResourcesTableAdapter.Fill(this.dataResources.wmf_Resources);
//判断是否有数据,如果没有,提示并退出
if (this.dataResources.wmf_ResourcesGroup.Rows.Count == 0)
{
this.Hide();
MessageBox.Show("资源组的根节点未建立,请先运行在Web项目下的App_Data下的BaseData.sql,建立起基本的数据, 再打开本系统", "RMF:基本数据尚未建立", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Dispose();
//退出程序
Application.Exit();
}
//绑定资源树
trv_Resources.Nodes.Clear();
new ResourcesBLL().ResourcesTreeBind("", null, dataResources.wmf_ResourcesGroup, trv_Resources);
trv_Resources.ExpandAll();
//绑定权限表
Privilegebind();
}
/// <summary>
/// 选择树以后,重新绑定列表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void trv_Resources_AfterSelect(object sender, TreeViewEventArgs e)
{
DataSelectByTree();
Privilegebind();
}
//绑定权限表
private void Privilegebind()
{
privilegeInited = false;
/*this.dataResources.wmf_Resources.Clear();
this.dataResources.wmf_ResourcesGroup.Clear();
this.dataResources.wmf_Operation.Clear();
this.dataResources.wmf_Privilege.Clear();
// TODO: 这行代码将数据加载到表“dataResources.wmf_Privilege”中。
this.wmf_PrivilegeTableAdapter.Fill(this.dataResources.wmf_Privilege);
// TODO: 这行代码将数据加载到表“dataResources.wmf_Operation”中。
this.wmf_OperationTableAdapter.Fill(this.dataResources.wmf_Operation);
// TODO: 这行代码将数据加载到表“dataResources.wmf_ResourcesGroup”中。
this.wmf_ResourcesGroupTableAdapter.Fill(this.dataResources.wmf_ResourcesGroup);
// TODO: 这行代码将数据加载到表“dataResources.wmf_Resources”中。
this.wmf_ResourcesTableAdapter.Fill(this.dataResources.wmf_Resources);*/
new PrivilegeBLL().PrivilegeBind(trv_Resources.SelectedNode.Name, this.dataResources.wmf_Operation, this.dataResources.wmf_Resources, this.dataResources.wmf_Privilege, dg_Privilege);
privilegeInited = true;
}
/// <summary>
/// 根据树选择数据内容
/// </summary>
private void DataSelectByTree()
{
try
{
this.dataResources.wmf_Privilege.Clear();
// TODO: 这行代码将数据加载到表“dataResources.wmf_Privilege”中。
this.wmf_PrivilegeTableAdapter.Fill(this.dataResources.wmf_Privilege);
switch (tabControl1.SelectedTab.Name)
{
case "tabPageResGroup":
wmfResourcesBindingSource.Filter = string.Empty;
wmfResourcesGroupBindingSource.Filter = "[ParentId]='" + trv_Resources.SelectedNode.Name + "'";
break;
case "tabPagePrivilege":
break;
case "tabPageResManage":
wmfResourcesGroupBindingSource.Filter = string.Empty;
wmfResourcesBindingSource.Filter = "[ResourceGroupId]='" + trv_Resources.SelectedNode.Name + "'";
break;
case "tabOperation":
break;
default:
break;
}
//过滤Privilege,包括tabPageResGroup(权限组管理)都要过滤
if (toolStripDisplayPrivilege.Checked && ((tabControl1.SelectedTab.Name == "tabPageResGroup") || (tabControl1.SelectedTab.Name == "tabPagePrivilege")))
{
StringBuilder privilegeFilter = new StringBuilder("Resourcesid = '");
foreach (DataResources.wmf_ResourcesRow resRow in this.dataResources.wmf_Resources.Select("ResourceGroupId='" + trv_Resources.SelectedNode.Name + "'"))
{
privilegeFilter.Append(resRow.ResourcesId + "' or Resourcesid = '");
}
int lastOr = privilegeFilter.ToString().LastIndexOf("or");
if (lastOr > 0)
{
privilegeFilter.Remove(lastOr, privilegeFilter.Length - lastOr);
}
else
{
privilegeFilter.Remove(0, privilegeFilter.Length);
}
this.wmfPrivilegeBindingSource.Filter = privilegeFilter.ToString();
lbl_TableName.Text = "与[" + trv_Resources.SelectedNode.Text + "]权限组(ResourceGroup)相关的权限记录,共 " + wmfPrivilegeBindingSource.Count.ToString() + " 条";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw;
}
}
#endregion
#region 工具以及菜单栏按钮事件
private void btn_ResReload_Click(object sender, EventArgs e)
{
//重新初始化
InitForm();
}
private void 重新载入RToolStripMenuItem_Click(object sender, EventArgs e)
{
//重新初始化
InitForm();
}
private void btn_Close_MouseEnter(object sender, EventArgs e)
{
btn_Close.FlatStyle = FlatStyle.Popup;
}
private void btn_Close_MouseLeave(object sender, EventArgs e)
{
btn_Close.FlatStyle = FlatStyle.Flat;
}
private void btn_Close_Click(object sender, EventArgs e)
{
panel1.Visible = false;
toolStripDisplayTree.Checked = false;
}
private void toolStripDisplayTree_CheckedChanged(object sender, EventArgs e)
{
panel1.Visible = toolStripDisplayTree.Checked;
}
private void toolStripLog_CheckedChanged(object sender, EventArgs e)
{
txt_OptLog.Visible = toolStripLog.Checked;
splitterLog.Visible = toolStripLog.Checked;
}
private void toolStripDisplayPrivilege_CheckedChanged(object sender, EventArgs e)
{
panel2.Visible = toolStripDisplayPrivilege.Checked;
splitterPrivilege.Visible = toolStripDisplayPrivilege.Checked;
}
private void 权限组ToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripDisplayTree.Checked = 权限组ToolStripMenuItem.Checked;
}
private void 日志ToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripLog.Checked = 日志ToolStripMenuItem.Checked;
}
private void 权限ToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripDisplayPrivilege.Checked = 权限ToolStripMenuItem.Checked;
}
private void 保存当前文件SToolStripMenuItem_Click_1(object sender, EventArgs e)
{
btn_ResSave_Click(sender, e);
}
/// <summary>
/// 让关闭按钮随着Splitter移动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void splitter1_SplitterMoved(object sender, SplitterEventArgs e)
{
btn_Close.Location = new Point(e.X - 25, btn_Close.Location.Y);
}
private void 资源操作选择SToolStripMenuItem_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabControl1.TabPages["tabPagePrivilege"];
}
private void 资源管理RToolStripMenuItem_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabControl1.TabPages["tabPageResManage"];
}
private void 权限组编辑GToolStripMenuItem_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabControl1.TabPages["tabPageResGroup"];
}
private void 操作类型管理OToolStripMenuItem_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabControl1.TabPages["tabOperation"];
}
private void 枚举代码生成EToolStripMenuItem_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabControl1.TabPages["tabPageCodeGen"];
}
#endregion
#region Guid相关功能
private void toolStripBtn_GenGuid_Click(object sender, EventArgs e)
{
//生成Guid
toolStripTxt_Guid.Text = Guid.NewGuid().ToString();
}
private void toolStripTxt_Guid_DoubleClick(object sender, EventArgs e)
{
toolStripTxt_Guid.SelectAll();
}
#endregion
#region 保存数据
/// <summary>
/// 保存按钮,按照当前显示的内容不同,保存不同的数据。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_ResSave_Click(object sender, EventArgs e)
{
try
{
switch (tabControl1.SelectedTab.Name)
{
case "tabPageResManage":
this.wmf_ResourcesTableAdapter.Update(this.dataResources.wmf_Resources);
MessageBox.Show("资源[Resources]更新成功", tabControl1.SelectedTab.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case "tabPageResGroup":
this.wmf_ResourcesGroupTableAdapter.Update(this.dataResources.wmf_ResourcesGroup);
MessageBox.Show("权限组[ResourcesGroup]更新成功", tabControl1.SelectedTab.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case "tabOperation":
this.wmf_OperationTableAdapter.Update(this.dataResources.wmf_Operation);
MessageBox.Show("操作类型[Operation]更新成功", tabControl1.SelectedTab.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case "tabPageCodeGen":
SaveFile();
break;
default:
MessageBox.Show("当前的数据为自动保存", "系统提示:", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
break;
}
InitForm();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Privilege中checkbox发生改变后保存相关数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dg_Privilege_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (!privilegeInited)
{
return;
}
string[] optTagValue = dg_Privilege.Columns[e.ColumnIndex].HeaderCell.Tag.ToString().Split('|');
string[] resTagValue = dg_Privilege.Rows[e.RowIndex].HeaderCell.Tag.ToString().Split('|');
int operationId = Convert.ToInt32(optTagValue[0]);
Guid resourcesId = new Guid(resTagValue[0]);
string operationValue = dg_Privilege.Columns[e.ColumnIndex].HeaderCell.Value.ToString();
string resourcesValue = dg_Privilege.Rows[e.RowIndex].HeaderCell.Value.ToString();
//如果值为假,就删除在数据库里面相应的记录
if (Convert.ToBoolean(dg_Privilege[e.ColumnIndex, e.RowIndex].Value))
{
SysLog("增加记录:" + operationValue + "+" + resourcesValue, true);
this.wmf_PrivilegeTableAdapter.Insert(Guid.NewGuid(), operationId, resourcesId, resTagValue[1] + optTagValue[1], resourcesValue + operationValue, null, null, 0);
}
else
{
//要删除以前,要把PrivilegeInRoles里面的东西一并删除
new PrivilegeInRolesBLL().DeletePrivilegeInRoles(operationId, resourcesId);
this.wmf_PrivilegeTableAdapter.DeleteQuery(operationId, resourcesId);
SysLog("删除权限记录:" + operationValue + "+" + resourcesValue, true);
}
}
#endregion
#region ContextMenu事件
private void 新建组AToolStripMenuItem_Click(object sender, EventArgs e)
{
AddNewGroup();
}
private void 重命名RToolStripMenuItem_Click(object sender, EventArgs e)
{
trv_Resources.SelectedNode.BeginEdit();
}
private void 删除DToolStripMenuItem_Click(object sender, EventArgs e)
{
//不能删除根节点,否则会出错
if (trv_Resources.SelectedNode.Text == "所有资源")
{
MessageBox.Show("不能删除根节点", "删除权限组", MessageBoxButtons.OK, MessageBoxIcon.Stop);
return;
}
if (MessageBox.Show("是否删除权限组[" + trv_Resources.SelectedNode.Text + "]以及下属权限", "删除权限组提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
DeleteResourceGroup(trv_Resources.SelectedNode);
}
}
private void 全部选择ToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (DataGridViewCell cell in dg_Privilege.SelectedCells)
{
cell.Value = true;
}
}
private void 全部取消ToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (DataGridViewCell cell in dg_Privilege.SelectedCells)
{
cell.Value = false;
}
}
private void 反向选择ToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (DataGridViewCell cell in dg_Privilege.SelectedCells)
{
cell.Value = !Convert.ToBoolean(cell.Value);
}
}
private void ts_Add_Click(object sender, EventArgs e)
{
AddNewGroup();
}
private void 修改名字ToolStripMenuItem_Click(object sender, EventArgs e)
{
trv_Resources.SelectedNode.BeginEdit();
}
#endregion
#region 代码生成
private void btn_SaveFile_Click(object sender, EventArgs e)
{
SaveFile();
}
private void btn_Resource_Click(object sender, EventArgs e)
{
txtIcs_Content.Text = CodeGen.ResourceCode();
lbl_FileName.Text = "Privilege.cs";
}
public void SaveFile()
{
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.Filter = "C# 文件(*.cs)|*.cs";
saveFile.Title = ("保存WMFConfig工具自动生成的代码文件");
saveFile.FileName = lbl_FileName.Text;
saveFile.ShowDialog();
if (saveFile.FileName != "")
{
string fileName = saveFile.FileName;
txtIcs_Content.SaveFile(fileName);
}
}
private void 保存当前文件SToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFile();
}
#endregion
#region TreeVie相关操作
/// <summary>
/// 编辑节点
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void trv_Resources_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
if (e.Label == null)
{
//如果是新节点
if (e.Node.Tag != null)
{
e.Node.Remove();
}
return;
}
if (e.Label != null && e.Label.Trim() == string.Empty && e.Label != e.Node.Text)
{
e.CancelEdit = true;
}
else
{
//如果是新节点
if (e.Node.Tag != null)
{
wmf_ResourcesGroupTableAdapter.Insert(new Guid(e.Node.Name), e.Label.Trim(), new Guid(e.Node.Parent.Name), "", e.Node.Level);
SysLog("添加[" + e.Label + "]节点成功!!", true);
e.Node.Tag = null;
//trv_Resources.SelectedNode = e.Node;
}
else
{
//更新到数据库
if (wmf_ResourcesGroupTableAdapter.UpdateGroupName(e.Label, e.Node.Text) == 1)
{
SysLog("名字[" + e.Node.Text + "]更新为[" + e.Label + "]成功!!", true);
toolStripStatus.Text = DateTime.Now.ToString() + " 更新权限组节点名称成功";
}
else
{
toolStripStatus.Text = "警告:更新权限组节点名称失败!!";
}
}
}
}
/// <summary>
/// 键盘操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void trv_Resources_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
//编辑节点
if (e.KeyCode == Keys.F2)
{
trv_Resources.SelectedNode.BeginEdit();
}
//编辑节点
if (e.KeyCode == Keys.F3)
{
AddNewGroup();
}
//删除节点
if (e.KeyCode == Keys.Delete)
{
if (MessageBox.Show("是否删除权限组[" + trv_Resources.SelectedNode.Text + "]以及下属权限", "删除权限组提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
DeleteResourceGroup(trv_Resources.SelectedNode);
}
}
}
/// <summary>
/// 删除已选择的权限组
/// </summary>
private void DeleteResourceGroup(TreeNode selectNode)
{
try
{
///遍历,把子节点全部删除
foreach (TreeNode child in selectNode.Nodes)
{
DeleteResourceGroup(child);
}
//删除Resources
DataResources.wmf_ResourcesRow[] resourceRows = (DataResources.wmf_ResourcesRow[])this.dataResources.wmf_Resources.Select("[ResourceGroupId]='" + selectNode.Name + "'");
foreach (MemberShipDAL.DataObject.DataResources.wmf_ResourcesRow resourceRow in resourceRows)
{
DataResources.wmf_PrivilegeRow[] privilegeRows = (DataResources.wmf_PrivilegeRow[])this.dataResources.wmf_Privilege.Select("[ResourcesId]='" + resourceRow.ResourcesId + "'");
foreach (DataResources.wmf_PrivilegeRow privilegeRow in privilegeRows)
{
//删除PrivilegeInRoles
new PrivilegeInRolesBLL().DeletePrivilegeInRoles(privilegeRow.PrivilegeId);
}
//删除Privilege
wmf_PrivilegeTableAdapter.DeleteByResourceId(resourceRow.ResourcesId);
}
wmf_ResourcesTableAdapter.DeleteByGroupId(new Guid(selectNode.Name));
wmf_ResourcesGroupTableAdapter.DeleteByGroupId(new Guid(selectNode.Name));
selectNode.Remove();
SysLog("删除节点[" + selectNode.Name + "]成功", true);
}
catch (Exception)
{
toolStripStatus.Text = "警告:删除失败!!";
throw;
}
}
private void trv_Resources_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
trv_Resources.SelectedNode = e.Node;
}
}
public void AddNewGroup()
{
TreeNode newNode = new TreeNode("新建权限组");
newNode.Name = Guid.NewGuid().ToString();
newNode.Tag = "NewGroup";
trv_Resources.SelectedNode.Nodes.Add(newNode);
trv_Resources.SelectedNode.Expand();
newNode.BeginEdit();
}
#endregion
#region TreeView拖拽功能
private void trv_Resources_ItemDrag(object sender, ItemDragEventArgs e)
{
// 获得选择的节点
this.dragNode = (TreeNode)e.Item;
this.trv_Resources.SelectedNode = this.dragNode;
// 为了拖拽得图像,重置 image list
this.imageListDrag.Images.Clear();
this.imageListDrag.ImageSize = new Size(this.dragNode.Bounds.Size.Width + this.trv_Resources.Indent, this.dragNode.Bounds.Height);
// 创建一个新的图像
//这个图像包含了要拖拽的节点
Bitmap bmp = new Bitmap(this.dragNode.Bounds.Width + this.trv_Resources.Indent, this.dragNode.Bounds.Height);
// 从bitmap获取Graphics
Graphics gfx = Graphics.FromImage(bmp);
// 绘制节点图标到bitmap
gfx.DrawImage(this.imgFolder.Images[0], 0, 0);
// 绘制节点名称到图片
gfx.DrawString(this.dragNode.Text,
this.trv_Resources.Font,
new SolidBrush(this.trv_Resources.ForeColor),
(float)this.trv_Resources.Indent, 1.0f);
//把图片加入ImageList
this.imageListDrag.Images.Add(bmp);
// 获得客户端鼠标节点
Point p = this.trv_Resources.PointToClient(Control.MousePosition);
//计算鼠标和节点边缘的距离
int dx = p.X + this.trv_Resources.Indent - this.dragNode.Bounds.Left - 15;
int dy = p.Y - this.dragNode.Bounds.Top + 48;
// 开始拖拽图片
if (DragHelper.ImageList_BeginDrag(this.imageListDrag.Handle, 0, dx, dy))
{
// 开始拖拽
this.trv_Resources.DoDragDrop(bmp, DragDropEffects.Move);
// 停止拖拽
DragHelper.ImageList_EndDrag();
}
}
private void trv_Resources_DragDrop(object sender, DragEventArgs e)
{
DragHelper.ImageList_DragLeave(this.trv_Resources.Handle);
//获得拖拽的节点
TreeNode dropNode = this.trv_Resources.GetNodeAt(this.trv_Resources.PointToClient(new Point(e.X, e.Y)));
// 如果目标节点不等于本身,就添加到其子节点
if (this.dragNode != dropNode)
{
if (dropNode.Name != dragNode.Parent.Name)
{
//在此把数据库更新
if (this.wmf_ResourcesGroupTableAdapter.UpdateParentId(new Guid(dropNode.Name), new Guid(dragNode.Name)) == 1)
{
//提示成功
SysLog("移动[" + dragNode.Text + "]到[" + dropNode.Text + "]成功!!", true);
toolStripStatus.Text = DateTime.Now.ToString() + "节点移动成功!!";
}
else
{
toolStripStatus.Text = DateTime.Now.ToString() + "节点移动失败";
}
}
else
{
MessageBox.Show("无法移动[" + dragNode.Text + "],目标节点和源节点相同", "移动节点时出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// 取出拖拽节点的父节点
if (this.dragNode.Parent == null)
{
this.trv_Resources.Nodes.Remove(this.dragNode);
}
else
{
this.dragNode.Parent.Nodes.Remove(this.dragNode);
}
// 添加节点到目标节点
dropNode.Nodes.Add(this.dragNode);
dropNode.Expand();
// 把拖拽的节点设为空
this.dragNode = null;
//取消滚动的计时器
this.timerExpand.Enabled = false;
this.timer.Enabled = false;
}
}
private void trv_Resources_DragEnter(object sender, DragEventArgs e)
{
DragHelper.ImageList_DragEnter(this.trv_Resources.Handle, e.X - this.trv_Resources.Left, e.Y - this.trv_Resources.Top);
// 滚动
this.timer.Enabled = true;
this.timerExpand.Enabled = true;
}
private void trv_Resources_DragLeave(object sender, EventArgs e)
{
DragHelper.ImageList_DragLeave(this.trv_Resources.Handle);
// 停止滚动
this.timer.Enabled = false;
this.timerExpand.Enabled = false;
}
private void trv_Resources_DragOver(object sender, DragEventArgs e)
{
//计算位置并且移动图片
Point formP = this.PointToClient(new Point(e.X, e.Y));
DragHelper.ImageList_DragMove(formP.X - this.trv_Resources.Left, formP.Y - this.trv_Resources.Top);
// 获得真正的移动节点
TreeNode dropNode = this.trv_Resources.GetNodeAt(this.trv_Resources.PointToClient(new Point(e.X, e.Y)));
if (dropNode == null)
{
e.Effect = DragDropEffects.None;
return;
}
e.Effect = DragDropEffects.Move;
// 如果鼠标在新的节点上,那就选中
if (this.tempDropNode != dropNode)
{
DragHelper.ImageList_DragShowNolock(false);
this.trv_Resources.SelectedNode = dropNode;
DragHelper.ImageList_DragShowNolock(true);
tempDropNode = dropNode;
}
//避免移动到自己的子节点上面
TreeNode tmpNode = dropNode;
while (tmpNode.Parent != null)
{
if (tmpNode.Parent == this.dragNode)
e.Effect = DragDropEffects.None;
tmpNode = tmpNode.Parent;
}
}
private void trv_Resources_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
if (e.Effect == DragDropEffects.Move)
{
// 在拖拽的时候,显示鼠标图标
e.UseDefaultCursors = false;
this.trv_Resources.Cursor = Cursors.Default;
}
else e.UseDefaultCursors = true;
}
private void timer_Tick(object sender, EventArgs e)
{
// 获得鼠标所在位置的节点
Point pt = PointToClient(Control.MousePosition);
TreeNode node = this.trv_Resources.GetNodeAt(pt);
if (node == null) return;
// 如果鼠标靠近上面,就向上滚动
if (pt.Y < 30)
{
// 选择上面一个节点
if (trv_Resources.TopNode.PrevNode != null)
{
node = trv_Resources.TopNode.PrevNode;
// 隐藏拖拽的图片
DragHelper.ImageList_DragShowNolock(false);
// 拖拽并且刷新
node.EnsureVisible();
this.trv_Resources.Refresh();
// 显示图片
DragHelper.ImageList_DragShowNolock(true);
}
}
// 如果鼠标靠近下面,就向下滚动
else if (pt.Y > this.trv_Resources.Size.Height - 30)
{
if (node.NextVisibleNode != null)
{
node = node.NextVisibleNode;
DragHelper.ImageList_DragShowNolock(false);
node.EnsureVisible();
this.trv_Resources.Refresh();
DragHelper.ImageList_DragShowNolock(true);
}
}
}
void timerExpand_Tick(object sender, EventArgs e)
{
if (trv_Resources.SelectedNode != null)
trv_Resources.SelectedNode.Expand();
}
#endregion
#region DataGridView错误提示
private void dg_Resources_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
MessageBox.Show("您插入的数据有误,详细信息如下:/n" + e.Exception.Message, "插入数据错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void dg_Operate_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
MessageBox.Show("您插入的数据有误,详细信息如下:/n" + e.Exception.Message, "插入数据错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
#endregion
#region Resource资源组的DataGridView事件
/// <summary>
/// 自动把中文名复职到英文名单元格内
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dg_Resources_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
dg_Resources_CellEndEdit(sender, e);
}
private void dg_Resources_CellLeave(object sender, DataGridViewCellEventArgs e)
{
dg_Resources_CellEndEdit(sender, e);
}
/// <summary>
/// 单元格快捷键
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dg_Resources_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
//Ctrl+S,保存当前记录
if (e.KeyData == (Keys.Control | Keys.S))
btn_ResSave_Click(sender, e);
}
private void dg_Resources_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
string columnCNName = "resourcesCNNameDataGridViewTextBoxColumn";
string columnName = "resourcesNameDataGridViewTextBoxColumn";
string copyValue;
if ((e.RowIndex > -1) && (e.ColumnIndex > -1) && (dg_Resources.Rows[e.RowIndex].Cells[columnCNName].Value != null))
{
copyValue = dg_Resources.Rows[e.RowIndex].Cells[columnCNName].Value.ToString().Trim();
if ((dg_Resources.Columns[e.ColumnIndex].Name == columnCNName) && (dg_Resources.Rows[e.RowIndex].Cells[columnName].Value.ToString() == string.Empty))
{
dg_Resources.Rows[e.RowIndex].Cells[columnName].Value = copyValue;
}
}
}
/// <summary>
/// 用户删除行的时候,要删除与其相关的记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dg_Resources_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
if ((!e.Row.IsNewRow) && (this.wmfPrivilegeBindingSource.Count != 0))
{
if (MessageBox.Show("请确认是否删除与[" + e.Row.Cells["resourcesCNNameDataGridViewTextBoxColumn"].Value.ToString() + "]相关的记录?", "删除相关记录", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
DeletePrivilegeByResourceId(new Guid(e.Row.Cells["resourcesIdDataGridViewTextBoxColumn"].Value.ToString()));
}
else
{
e.Cancel = true;
}
}
}
/// <summary>
/// 用户编辑行的时候,删除相关记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dg_Resources_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
this.wmfPrivilegeBindingSource.Filter = "[ResourcesId]='" + dg_Resources.Rows[e.RowIndex].Cells["resourcesIdDataGridViewTextBoxColumn"].Value.ToString() + "'";
if ((!dg_Resources.Rows[e.RowIndex].IsNewRow) && (this.wmfPrivilegeBindingSource.Count != 0) && dg_Resources.Columns[e.ColumnIndex].Name != "descriptionDataGridViewTextBoxColumn" && dg_Resources.Columns[e.ColumnIndex].Name != "ResourcesOrder" && dg_Resources.Columns[e.ColumnIndex].Name != "ResourceGroupId")
{
if (MessageBox.Show("在编辑关键信息以前,请确认是否删除与[" + dg_Resources["resourcesCNNameDataGridViewTextBoxColumn", e.RowIndex].Value.ToString() + "]相关的记录?", "编辑前删除相关记录", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
DeletePrivilegeByResourceId(new Guid(dg_Resources["resourcesIdDataGridViewTextBoxColumn", e.RowIndex].Value.ToString()));
}
else
{
e.Cancel = true;
}
}
}
/// <summary>
/// 给列表添加自动生成GUID的功能
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dg_Resources_RowEnter(object sender, DataGridViewCellEventArgs e)
{
if (dg_Resources.Rows[e.RowIndex].IsNewRow)
{
dg_Resources.Rows[e.RowIndex].Cells["resourcesIdDataGridViewTextBoxColumn"].Value = Guid.NewGuid().ToString();
dg_Resources.Rows[e.RowIndex].Cells["ResourceGroupId"].Value = trv_Resources.SelectedNode.Name;
dg_Resources.Rows[e.RowIndex].Cells["ResourcesOrder"].Value = this.dataResources.wmf_Resources.Rows.Count + 1;
}
if (dg_Resources.Rows[e.RowIndex].Cells["resourcesIdDataGridViewTextBoxColumn"].Value != null)
{
// 过滤
this.wmfPrivilegeBindingSource.Filter = "[ResourcesId]='" + dg_Resources.Rows[e.RowIndex].Cells["resourcesIdDataGridViewTextBoxColumn"].Value.ToString() + "'";
lbl_TableName.Text = "与资源[" + dg_Resources.Rows[e.RowIndex].Cells["resourcesCNNameDataGridViewTextBoxColumn"].Value.ToString() + "]相关的权限记录,共 " + wmfPrivilegeBindingSource.Count.ToString() + " 条记录";
}
}
/// <summary>
/// 根据资源删除相关的权限
/// </summary>
/// <param name="operationId"></param>
private void DeletePrivilegeByResourceId(Guid resourceID)
{
//检验这个权限是否被使用,如果有被使用,则删除
foreach (DataResources.wmf_PrivilegeRow row in wmf_PrivilegeTableAdapter.GetDataByResourceId(resourceID))
{
//删除PrivilegeInRoles
new PrivilegeInRolesBLL().DeletePrivilegeInRoles(row.ResourcesId);
}
int i = wmf_PrivilegeTableAdapter.DeleteByResourceId(resourceID);
//重新加载数据
this.dataResources.wmf_Privilege.Clear();
wmf_PrivilegeTableAdapter.Fill(dataResources.wmf_Privilege);
SysLog("成功删除了" + i.ToString() + "条与resourceID[" + resourceID + "]相关的记录", true);
}
#endregion
#region Operate操作类型的DataGridView事件
private void dg_Operate_RowEnter(object sender, DataGridViewCellEventArgs e)
{
if (dg_Operate["operationIdDataGridViewTextBoxColumn", e.RowIndex].Value != null)
{
// 过滤
this.wmfPrivilegeBindingSource.Filter = "[OperationID]=" + dg_Operate["operationIdDataGridViewTextBoxColumn", e.RowIndex].Value.ToString();
lbl_TableName.Text = "与[" + dg_Operate["OperationCNName", e.RowIndex].Value.ToString() + "]操作(Operation)相关的权限记录共 " + wmfPrivilegeBindingSource.Count.ToString() + " 条记录";
}
}
private void dg_Operate_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
if ((!dg_Operate.Rows[e.RowIndex].IsNewRow) && (this.wmfPrivilegeBindingSource.Count != 0) && dg_Operate.Columns[e.ColumnIndex].Name != "descriptionDataGridViewTextBoxColumn2")
{
if (MessageBox.Show("在编辑关键信息以前,请确认是否删除与[" + dg_Operate["OperationCNName", e.RowIndex].Value.ToString() + "]相关的记录?", "编辑前删除相关记录", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
DeletePrivilegeByOperationId((int)dg_Operate["operationIdDataGridViewTextBoxColumn", e.RowIndex].Value);
}
else
{
e.Cancel = true;
}
}
}
/// <summary>
/// 根据操作类型删除相关的权限
/// </summary>
/// <param name="operationId"></param>
private void DeletePrivilegeByOperationId(int operationId)
{
//检验这个权限是否被使用,如果有被使用,则删除
foreach (DataResources.wmf_PrivilegeRow row in wmf_PrivilegeTableAdapter.GetDataByOperationID(operationId))
{
//删除PrivilegeInRoles
new PrivilegeInRolesBLL().DeletePrivilegeInRoles(row.PrivilegeId);
}
int i = wmf_PrivilegeTableAdapter.DeleteByOperationId(operationId);
//重新加载数据
this.dataResources.wmf_Privilege.Clear();
wmf_PrivilegeTableAdapter.Fill(dataResources.wmf_Privilege);
SysLog("成功删除了" + i.ToString() + "条与OperationID[" + operationId + "]相关的记录", true);
}
/// <summary>
/// 删除行,要删除相关记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dg_Operate_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
if ((!e.Row.IsNewRow) && (this.wmfPrivilegeBindingSource.Count != 0))
{
if (MessageBox.Show("在删除操作之前,请确认是否删除与此相关的信息", "删除相关记录", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
DeletePrivilegeByOperationId((int)e.Row.Cells["operationIdDataGridViewTextBoxColumn"].Value);
}
else
{
e.Cancel = true;
}
}
}
#endregion
#region 清理冗余数据
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripClean_Click(object sender, EventArgs e)
{
try
{
SysLog("开始清理在Privilege表中重复的数据", true);
int i = this.wmf_PrivilegeTableAdapter.DeleteRepeatData();
SysLog("清理在Privilege表中重复的数据成功!", true);
MessageBox.Show("清理冗余数据成功!", "清理冗余数据", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
SysLog("清理冗余数据数据失败,原因是:" + ex.Message, true);
MessageBox.Show("清理冗余数据失败,原因是:" + ex.Message, "清理冗余数据", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
#endregion
}
}
转自:http://www.koders.com/csharp/fid0EF67F8DA066B9DCC4804ED3E30AC8EFD8E93D85.aspx#L1032