/// 初始化TreeView
/// </summary>
private void InitTreeview()
{
try
{
treeView1.CheckBoxes = true;
treeView1.Nodes.Clear();
dt_menu=new DataTable("dt_rightTable");
dt_menu.Columns.Add("ID", typeof(String));
dt_menu.Columns.Add("RightName", typeof(String));
dt_menu.Columns.Add("ParentID", typeof(String));
DataRow dr;
增加权限数据表#region 增加权限数据表
资源管理#region 资源管理
dr=dt_menu.NewRow();
dr["ID"]="1";
dr["RightName"]="资源管理";
dr["ParentID"]="0";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="11";
dr["RightName"]="查询";
dr["ParentID"]="1";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="12";
dr["RightName"]="增加";
dr["ParentID"]="1";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="13";
dr["RightName"]="修改";
dr["ParentID"]="1";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="14";
dr["RightName"]="删除";
dr["ParentID"]="1";
dt_menu.Rows.Add(dr);
#endregion
资源中心#region 资源中心
dr=dt_menu.NewRow();
dr["ID"]="2";
dr["RightName"]="资源中心";
dr["ParentID"]="0";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="21";
dr["RightName"]="查询";
dr["ParentID"]="2";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="22";
dr["RightName"]="增加";
dr["ParentID"]="2";
dt_menu.Rows.Add(dr);
#endregion
决策支持#region 决策支持
dr=dt_menu.NewRow();
dr["ID"]="3";
dr["RightName"]="决策支持";
dr["ParentID"]="0";
dt_menu.Rows.Add(dr);
地图导航#region 地图导航
dr=dt_menu.NewRow();
dr["ID"]="31";
dr["RightName"]="地图导航";
dr["ParentID"]="3";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="311";
dr["RightName"]="编辑";
dr["ParentID"]="31";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="312";
dr["RightName"]="查询";
dr["ParentID"]="31";
dt_menu.Rows.Add(dr);
#endregion
数据报表#region 数据报表
dr=dt_menu.NewRow();
dr["ID"]="32";
dr["RightName"]="数据报表";
dr["ParentID"]="3";
dt_menu.Rows.Add(dr);
旅游参观人数统计#region 旅游参观人数统计
dr=dt_menu.NewRow();
dr["ID"]="321";
dr["RightName"]="旅游参观人数统计";
dr["ParentID"]="32";
dt_menu.Rows.Add(dr);
#endregion
社会无力维修文物专项补助经费统计表#region 社会无力维修文物专项补助经费统计表
dr=dt_menu.NewRow();
dr["ID"]="322";
dr["RightName"]="社会无力维修文物专项补助经费统计表";
dr["ParentID"]="32";
dt_menu.Rows.Add(dr);
#endregion
古村落、古建筑保护维修情况统计表#region 古村落、古建筑保护维修情况统计表
dr=dt_menu.NewRow();
dr["ID"]="323";
dr["RightName"]="古村落、古建筑保护维修情况统计表";
dr["ParentID"]="32";
dt_menu.Rows.Add(dr);
#endregion
文物保护专项经费使用情况报表#region 文物保护专项经费使用情况报表
dr=dt_menu.NewRow();
dr["ID"]="324";
dr["RightName"]="文物保护专项经费使用情况报表";
dr["ParentID"]="32";
dt_menu.Rows.Add(dr);
#endregion
举报问题统计表#region 举报问题统计表
dr=dt_menu.NewRow();
dr["ID"]="325";
dr["RightName"]="举报问题统计表";
dr["ParentID"]="32";
dt_menu.Rows.Add(dr);
#endregion
发文管理#region 发文管理
dr=dt_menu.NewRow();
dr["ID"]="326";
dr["RightName"]="发文管理";
dr["ParentID"]="32";
dt_menu.Rows.Add(dr);
#endregion
收文管理#region 收文管理
dr=dt_menu.NewRow();
dr["ID"]="327";
dr["RightName"]="收文管理";
dr["ParentID"]="32";
dt_menu.Rows.Add(dr);
#endregion
#endregion
#endregion
工作信息#region 工作信息
dr=dt_menu.NewRow();
dr["ID"]="4";
dr["RightName"]="工作信息";
dr["ParentID"]="0";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="41";
dr["RightName"]="查询";
dr["ParentID"]="4";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="42";
dr["RightName"]="增加";
dr["ParentID"]="4";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="43";
dr["RightName"]="修改";
dr["ParentID"]="4";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="44";
dr["RightName"]="删除";
dr["ParentID"]="4";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="45";
dr["RightName"]="摸板维护";
dr["ParentID"]="4";
dt_menu.Rows.Add(dr);
#endregion
工作计划#region 工作计划
dr=dt_menu.NewRow();
dr["ID"]="5";
dr["RightName"]="工作计划";
dr["ParentID"]="0";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="51";
dr["RightName"]="查询";
dr["ParentID"]="5";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="52";
dr["RightName"]="删除";
dr["ParentID"]="5";
dt_menu.Rows.Add(dr);
#endregion
工作动态#region 工作动态
dr=dt_menu.NewRow();
dr["ID"]="6";
dr["RightName"]="工作动态";
dr["ParentID"]="0";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="61";
dr["RightName"]="查询";
dr["ParentID"]="6";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="62";
dr["RightName"]="删除";
dr["ParentID"]="6";
dt_menu.Rows.Add(dr);
#endregion
科研项目#region 科研项目
dr=dt_menu.NewRow();
dr["ID"]="7";
dr["RightName"]="科研项目";
dr["ParentID"]="0";
dt_menu.Rows.Add(dr);
建筑评估#region 建筑评估
dr=dt_menu.NewRow();
dr["ID"]="71";
dr["RightName"]="建筑评估";
dr["ParentID"]="7";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="711";
dr["RightName"]="查看";
dr["ParentID"]="71";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="712";
dr["RightName"]="编辑";
dr["ParentID"]="71";
dt_menu.Rows.Add(dr);
#endregion
完好率评测#region 完好率评测
dr=dt_menu.NewRow();
dr["ID"]="72";
dr["RightName"]="完好率评测";
dr["ParentID"]="7";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="721";
dr["RightName"]="查看";
dr["ParentID"]="72";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="722";
dr["RightName"]="编辑";
dr["ParentID"]="72";
dt_menu.Rows.Add(dr);
#endregion
#endregion
文保档案#region 文保档案
dr=dt_menu.NewRow();
dr["ID"]="8";
dr["RightName"]="文保档案";
dr["ParentID"]="0";
dt_menu.Rows.Add(dr);
记录档案#region 记录档案
dr=dt_menu.NewRow();
dr["ID"]="81";
dr["RightName"]="记录档案";
dr["ParentID"]="8";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="811";
dr["RightName"]="查看";
dr["ParentID"]="81";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="812";
dr["RightName"]="维护";
dr["ParentID"]="81";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="813";
dr["RightName"]="删除";
dr["ParentID"]="81";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="814";
dr["RightName"]="摸板管理";
dr["ParentID"]="81";
dt_menu.Rows.Add(dr);
#endregion
台帐管理#region 台帐管理
dr=dt_menu.NewRow();
dr["ID"]="82";
dr["RightName"]="台帐管理";
dr["ParentID"]="8";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="821";
dr["RightName"]="查看";
dr["ParentID"]="82";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="822";
dr["RightName"]="维护";
dr["ParentID"]="82";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="823";
dr["RightName"]="删除";
dr["ParentID"]="82";
dt_menu.Rows.Add(dr);
#endregion
#region
#endregion
#endregion
系统管理#region 系统管理
dr=dt_menu.NewRow();
dr["ID"]="9";
dr["RightName"]="系统管理";
dr["ParentID"]="0";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="91";
dr["RightName"]="查看";
dr["ParentID"]="9";
dt_menu.Rows.Add(dr);
dr=dt_menu.NewRow();
dr["ID"]="92";
dr["RightName"]="编辑";
dr["ParentID"]="9";
dt_menu.Rows.Add(dr);
#endregion
#endregion
AddTree(0, (TreeNode)null);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
其中用到方法:
/// 递归添加树的节点
/// </summary>
/// <param name="ParentID">父节点ID</param>
/// <param name="pNode">节点</param>
public void AddTree(int ParentID,TreeNode pNode)
{
DataView dvTree = new DataView(dt_menu);
dvTree.RowFilter = "[ParentID] = " + ParentID;
foreach(DataRowView Row in dvTree)
{
TreeNode Node=new TreeNode() ;
TreeNode node_temp=new TreeNode();
if(pNode == null)
{
//增加根节点
node_temp.Text =Row["RightName"].ToString();
node_temp.Tag =Row["ID"].ToString();
treeView1.Nodes.Add(node_temp);
AddTree(Int32.Parse(Row["ID"].ToString()),node_temp); //再次递归
}
else
{
node_temp.Text =Row["RightName"].ToString();
node_temp.Tag =Row["ID"].ToString();
pNode.Nodes.Add(node_temp);
AddTree(Int32.Parse(Row["ID"].ToString()),node_temp); //再次递归
}
}
}
在TreeView的AfterCheck事件里面编写如下代码:
{
//单击了treeView控件所触发的事件
//MessageBox.Show(e.Node.Index.ToString());
//如果夫节点下面有子节点则全部选中
if(e.Action != TreeViewAction.Unknown)
{
if(e.Node.Nodes.Count > 0)
{
this.CheckAllChildNodes(e.Node, e.Node.Checked);
}
//如果选中子节点同时选中父节点
if(e.Node.Checked == true)
{
this.selectParent(e.Node);
}
//如果取消子选择子节点,而且当所有本及子节点未选中时,取消父级节点
if(e.Node.Checked == false)
{
this.cancelParent(e.Node);
}
}
}
使用到的方法:
{
foreach(TreeNode node in treeNode.Nodes)
{
node.Checked = nodeChecked;
if(node.Nodes.Count > 0)
{
this.CheckAllChildNodes(node, nodeChecked);
}
}
}
//取消上级节点
private void cancelParent(TreeNode node)
{
if(node.Parent != null)
{
bool isCheck = false;
foreach(TreeNode n in node.Parent.Nodes)
{
if(n.Checked == true)
isCheck = true;
}
if(!isCheck)
node.Parent.Checked = false;
}
}
//设置选中父节点
private void selectParent(TreeNode node)
{
if(node.Parent != null && node.Parent.Checked == false)
{
node.Parent.Checked = true;
if(node.Parent.Parent != null)
this.selectParent(node.Parent);
}
}
取消TreeView里面所有的钩
{
System.Windows.Forms.TreeNodeCollection nodes=treeView1.Nodes;
ClearSelectTree(nodes);
} //清除所有打勾选项
private void ClearSelectTree(System.Windows.Forms.TreeNodeCollection nodes)
{
foreach(System.Windows.Forms.TreeNode node in nodes)
{
node.Checked =false;
ClearSelectTree(node.Nodes); //递归查找
}
}
给每个TreeView里面节点的Tag加str_right 这里str_right是全局变量
/// 遍历treeview 调用GetAllNode(treeView1.Nodes)
/// </summary>
/// <param name="nodes"></param>
private void GetAllNode(System.Windows.Forms.TreeNodeCollection nodes)
{
foreach(System.Windows.Forms.TreeNode node in nodes)
{
if (node.Checked)
{
if (str_right!="")
{
str_right+=";"+node.Tag.ToString();
}
else
{
str_right=node.Tag.ToString();
}
}
GetAllNode(node.Nodes); //递归查找
}
}
下面判断是否打钩