白天有雨

此处应该有掌声

导航

C#中TreeView类操作全攻略(一)

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using com.prm.client.tools;
using System.Data.OracleClient;
using com.prm.client.common;
using com.prm.client.sysmanager.popedom;

namespace com.prm.client.forms
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class SM_FunctionMaintenance : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.TreeView trv_Function;
private System.Windows.Forms.Label lbl_Detail;
private System.Windows.Forms.Label lbl_BelongTo;
private System.Windows.Forms.Label lbl_FunEntity;
private System.Windows.Forms.Label lbl_Type;
private System.Windows.Forms.Label lbl_IsInterface;
private System.Windows.Forms.Label lbl_Tag;
private System.Windows.Forms.Label lbl_Name;
private System.Windows.Forms.CheckBox ckb_IsInterface;
private System.Windows.Forms.ComboBox cbo_Type;
private System.Windows.Forms.TextBox txt_Detail;
private System.Windows.Forms.TextBox txt_BelongTo;
private System.Windows.Forms.TextBox txt_FunEntity;
private System.Windows.Forms.TextBox txt_Tag;
private System.Windows.Forms.TextBox txt_Name;
private System.Data.DataSet dataSet1;
private System.Data.DataColumn dataColumn1;
private System.Data.DataColumn dataColumn2;
private System.Data.DataColumn dataColumn3;
private System.Data.DataColumn dataColumn4;
private System.Data.DataColumn dataColumn5;
private System.Data.DataColumn dataColumn6;
private System.Data.DataColumn dataColumn7;
private System.Data.DataColumn dataColumn8;
private System.Data.DataColumn dataColumn9;
private System.Data.DataColumn dataColumn10;
private System.Data.DataColumn dataColumn11;
//用来保存功能表中所有的数据
private System.Data.DataTable funcDataTable;

//用来保存Connection属性
private OracleConnection funcTableConn;
//用来保存数字字典功能类别的数据
ClientDictionary[] DIC_funcType;
//用来保存树总的虚拟根节点的ID,默认为0
public const string ROOT_NODE_VALUE = "0";
private System.Windows.Forms.Button btn_Help;
private System.Windows.Forms.Button btn_Remove;
private System.Windows.Forms.Button btn_Modify;
private System.Windows.Forms.Button btn_AddSon;
private System.Windows.Forms.Button btn_AddRoot;
private System.Windows.Forms.Button btn_Close;



//树结点的临时信息
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public SM_FunctionMaintenance(SM_Popedom popedom)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//生成类DataAccessObject的实例,并保存连接属性
DataAccessObject funcTableAccessObject=new DataAccessObject();
funcTableConn=funcTableAccessObject.Connection;
//调用QueryAllFuncInfo过程来对变量funcDataTable进行赋值
QueryAllFuncInfo();
const string VALUEMEMBER="valueMember",DISPLAYMEMBER="displayMember",NULLTEXT="";
//调用QueryFuncTypeDictionary过程来对变量DIC_funcType进行赋值
QueryFuncTypeDictionary();
cbo_Type.DataSource = DIC_funcType;
cbo_Type.ValueMember = VALUEMEMBER;
cbo_Type.DisplayMember = DISPLAYMEMBER;
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SM_FunctionMaintenance));
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.trv_Function = new System.Windows.Forms.TreeView();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.lbl_Detail = new System.Windows.Forms.Label();
this.lbl_BelongTo = new System.Windows.Forms.Label();
this.lbl_FunEntity = new System.Windows.Forms.Label();
this.lbl_Type = new System.Windows.Forms.Label();
this.lbl_IsInterface = new System.Windows.Forms.Label();
this.lbl_Tag = new System.Windows.Forms.Label();
this.lbl_Name = new System.Windows.Forms.Label();
this.cbo_Type = new System.Windows.Forms.ComboBox();
this.txt_Detail = new System.Windows.Forms.TextBox();
this.txt_BelongTo = new System.Windows.Forms.TextBox();
this.txt_FunEntity = new System.Windows.Forms.TextBox();
this.ckb_IsInterface = new System.Windows.Forms.CheckBox();
this.txt_Tag = new System.Windows.Forms.TextBox();
this.txt_Name = new System.Windows.Forms.TextBox();
this.dataSet1 = new System.Data.DataSet();
this.funcDataTable = new System.Data.DataTable();
this.dataColumn1 = new System.Data.DataColumn();
this.dataColumn2 = new System.Data.DataColumn();
this.dataColumn3 = new System.Data.DataColumn();
this.dataColumn4 = new System.Data.DataColumn();
this.dataColumn5 = new System.Data.DataColumn();
this.dataColumn6 = new System.Data.DataColumn();
this.dataColumn7 = new System.Data.DataColumn();
this.dataColumn8 = new System.Data.DataColumn();
this.dataColumn9 = new System.Data.DataColumn();
this.dataColumn10 = new System.Data.DataColumn();
this.dataColumn11 = new System.Data.DataColumn();
this.btn_Help = new System.Windows.Forms.Button();
this.btn_Remove = new System.Windows.Forms.Button();
this.btn_Modify = new System.Windows.Forms.Button();
this.btn_AddSon = new System.Windows.Forms.Button();
this.btn_AddRoot = new System.Windows.Forms.Button();
this.btn_Close = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.funcDataTable)).BeginInit();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.trv_Function);
this.groupBox1.Location = new System.Drawing.Point(24, 24);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(440, 496);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "功能列表:";
//
// trv_Function
//
this.trv_Function.AllowDrop = true;
this.trv_Function.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.trv_Function.ImageIndex = -1;
this.trv_Function.Indent = 40;
this.trv_Function.Location = new System.Drawing.Point(8, 16);
this.trv_Function.Name = "trv_Function";
this.trv_Function.SelectedImageIndex = -1;
this.trv_Function.Size = new System.Drawing.Size(424, 472);
this.trv_Function.TabIndex = 0;
this.trv_Function.DragOver += new System.Windows.Forms.DragEventHandler(this.trv_Function_DragOver);
this.trv_Function.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.trv_Function_AfterSelect);
this.trv_Function.DragEnter += new System.Windows.Forms.DragEventHandler(this.trv_Function_DragEnter);
this.trv_Function.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.trv_Function_ItemDrag);
this.trv_Function.DragDrop += new System.Windows.Forms.DragEventHandler(this.trv_Function_DragDrop);
//
// groupBox2
//
this.groupBox2.Controls.Add(this.lbl_Detail);
this.groupBox2.Controls.Add(this.lbl_BelongTo);
this.groupBox2.Controls.Add(this.lbl_FunEntity);
this.groupBox2.Controls.Add(this.lbl_Type);
this.groupBox2.Controls.Add(this.lbl_IsInterface);
this.groupBox2.Controls.Add(this.lbl_Tag);
this.groupBox2.Controls.Add(this.lbl_Name);
this.groupBox2.Controls.Add(this.cbo_Type);
this.groupBox2.Controls.Add(this.txt_Detail);
this.groupBox2.Controls.Add(this.txt_BelongTo);
this.groupBox2.Controls.Add(this.txt_FunEntity);
this.groupBox2.Controls.Add(this.ckb_IsInterface);
this.groupBox2.Controls.Add(this.txt_Tag);
this.groupBox2.Controls.Add(this.txt_Name);
this.groupBox2.Location = new System.Drawing.Point(472, 24);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(296, 496);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "功能详细信息:";
//
// lbl_Detail
//
this.lbl_Detail.Location = new System.Drawing.Point(56, 320);
this.lbl_Detail.Name = "lbl_Detail";
this.lbl_Detail.Size = new System.Drawing.Size(48, 23);
this.lbl_Detail.TabIndex = 13;
this.lbl_Detail.Text = "描述:";
//
// lbl_BelongTo
//
this.lbl_BelongTo.Location = new System.Drawing.Point(8, 272);
this.lbl_BelongTo.Name = "lbl_BelongTo";
this.lbl_BelongTo.Size = new System.Drawing.Size(94, 23);
this.lbl_BelongTo.TabIndex = 12;
this.lbl_BelongTo.Text = "所属窗口标识:";
//
// lbl_FunEntity
//
this.lbl_FunEntity.Location = new System.Drawing.Point(40, 224);
this.lbl_FunEntity.Name = "lbl_FunEntity";
this.lbl_FunEntity.Size = new System.Drawing.Size(64, 23);
this.lbl_FunEntity.TabIndex = 11;
this.lbl_FunEntity.Text = " 功能体:";
//
// lbl_Type
//
this.lbl_Type.Location = new System.Drawing.Point(56, 176);
this.lbl_Type.Name = "lbl_Type";
this.lbl_Type.Size = new System.Drawing.Size(48, 23);
this.lbl_Type.TabIndex = 10;
this.lbl_Type.Text = "类别:";
//
// lbl_IsInterface
//
this.lbl_IsInterface.Location = new System.Drawing.Point(32, 128);
this.lbl_IsInterface.Name = "lbl_IsInterface";
this.lbl_IsInterface.Size = new System.Drawing.Size(72, 23);
this.lbl_IsInterface.TabIndex = 9;
this.lbl_IsInterface.Text = "有无界面:";
//
// lbl_Tag
//
this.lbl_Tag.Location = new System.Drawing.Point(56, 88);
this.lbl_Tag.Name = "lbl_Tag";
this.lbl_Tag.Size = new System.Drawing.Size(48, 23);
this.lbl_Tag.TabIndex = 8;
this.lbl_Tag.Text = "标识:";
//
// lbl_Name
//
this.lbl_Name.Location = new System.Drawing.Point(56, 40);
this.lbl_Name.Name = "lbl_Name";
this.lbl_Name.Size = new System.Drawing.Size(48, 23);
this.lbl_Name.TabIndex = 7;
this.lbl_Name.Text = "名称:";
//
// cbo_Type
//
this.cbo_Type.Enabled = false;
this.cbo_Type.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.cbo_Type.Location = new System.Drawing.Point(104, 168);
this.cbo_Type.Name = "cbo_Type";
this.cbo_Type.Size = new System.Drawing.Size(176, 20);
this.cbo_Type.TabIndex = 6;
//
// txt_Detail
//
this.txt_Detail.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.txt_Detail.Location = new System.Drawing.Point(104, 312);
this.txt_Detail.Name = "txt_Detail";
this.txt_Detail.ReadOnly = true;
this.txt_Detail.Size = new System.Drawing.Size(176, 21);
this.txt_Detail.TabIndex = 5;
this.txt_Detail.Text = "";
//
// txt_BelongTo
//
this.txt_BelongTo.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.txt_BelongTo.Location = new System.Drawing.Point(104, 264);
this.txt_BelongTo.Name = "txt_BelongTo";
this.txt_BelongTo.ReadOnly = true;
this.txt_BelongTo.Size = new System.Drawing.Size(176, 21);
this.txt_BelongTo.TabIndex = 4;
this.txt_BelongTo.Text = "";
//
// txt_FunEntity
//
this.txt_FunEntity.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.txt_FunEntity.Location = new System.Drawing.Point(104, 216);
this.txt_FunEntity.Name = "txt_FunEntity";
this.txt_FunEntity.ReadOnly = true;
this.txt_FunEntity.Size = new System.Drawing.Size(176, 21);
this.txt_FunEntity.TabIndex = 3;
this.txt_FunEntity.Text = "";
//
// ckb_IsInterface
//
this.ckb_IsInterface.Enabled = false;
this.ckb_IsInterface.Location = new System.Drawing.Point(104, 120);
this.ckb_IsInterface.Name = "ckb_IsInterface";
this.ckb_IsInterface.Size = new System.Drawing.Size(24, 24);
this.ckb_IsInterface.TabIndex = 2;
//
// txt_Tag
//
this.txt_Tag.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.txt_Tag.Location = new System.Drawing.Point(104, 80);
this.txt_Tag.Name = "txt_Tag";
this.txt_Tag.ReadOnly = true;
this.txt_Tag.Size = new System.Drawing.Size(176, 21);
this.txt_Tag.TabIndex = 1;
this.txt_Tag.Text = "";
//
// txt_Name
//
this.txt_Name.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.txt_Name.Location = new System.Drawing.Point(104, 32);
this.txt_Name.Name = "txt_Name";
this.txt_Name.ReadOnly = true;
this.txt_Name.Size = new System.Drawing.Size(176, 21);
this.txt_Name.TabIndex = 0;
this.txt_Name.Text = "";
//
// dataSet1
//
this.dataSet1.DataSetName = "NewDataSet";
this.dataSet1.Locale = new System.Globalization.CultureInfo("zh-CN");
this.dataSet1.Tables.AddRange(new System.Data.DataTable[] {
this.funcDataTable});
//
// funcDataTable
//
this.funcDataTable.Columns.AddRange(new System.Data.DataColumn[] {
this.dataColumn1,
this.dataColumn2,
this.dataColumn3,
this.dataColumn4,
this.dataColumn5,
this.dataColumn6,
this.dataColumn7,
this.dataColumn8,
this.dataColumn9,
this.dataColumn10,
this.dataColumn11});
this.funcDataTable.TableName = "funcDataTable";
//
// dataColumn1
//
this.dataColumn1.ColumnName = "功能ID";
//
// dataColumn2
//
this.dataColumn2.ColumnName = "功能标识";
//
// dataColumn3
//
this.dataColumn3.ColumnName = "功能名称";
//
// dataColumn4
//
this.dataColumn4.ColumnName = "功能简述";
//
// dataColumn5
//
this.dataColumn5.ColumnName = "所属窗体";
//
// dataColumn6
//
this.dataColumn6.ColumnName = "有无界面";
//
// dataColumn7
//
this.dataColumn7.ColumnName = "功能类别";
//
// dataColumn8
//
this.dataColumn8.ColumnName = "功能体";
//
// dataColumn9
//
this.dataColumn9.ColumnName = "上级功能ID";
//
// dataColumn10
//
this.dataColumn10.ColumnName = "可否展显";
//
// dataColumn11
//
this.dataColumn11.ColumnName = "版本号";
//
// btn_Help
//
this.btn_Help.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btn_Help.BackgroundImage")));
this.btn_Help.Location = new System.Drawing.Point(536, 536);
this.btn_Help.Name = "btn_Help";
this.btn_Help.Size = new System.Drawing.Size(75, 22);
this.btn_Help.TabIndex = 10;
this.btn_Help.Text = "帮 助";
this.btn_Help.Click += new System.EventHandler(this.btn_Help_Click);
//
// btn_Remove
//
this.btn_Remove.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btn_Remove.BackgroundImage")));
this.btn_Remove.Location = new System.Drawing.Point(344, 536);
this.btn_Remove.Name = "btn_Remove";
this.btn_Remove.Size = new System.Drawing.Size(75, 22);
this.btn_Remove.TabIndex = 9;
this.btn_Remove.Text = "删 除";
this.btn_Remove.Click += new System.EventHandler(this.btn_Remove_Click);
//
// btn_Modify
//
this.btn_Modify.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btn_Modify.BackgroundImage")));
this.btn_Modify.Location = new System.Drawing.Point(248, 536);
this.btn_Modify.Name = "btn_Modify";
this.btn_Modify.Size = new System.Drawing.Size(75, 22);
this.btn_Modify.TabIndex = 8;
this.btn_Modify.Text = "修 改";
this.btn_Modify.Click += new System.EventHandler(this.btn_Modify_Click);
//
// btn_AddSon
//
this.btn_AddSon.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btn_AddSon.BackgroundImage")));
this.btn_AddSon.Location = new System.Drawing.Point(152, 536);
this.btn_AddSon.Name = "btn_AddSon";
this.btn_AddSon.Size = new System.Drawing.Size(80, 22);
this.btn_AddSon.TabIndex = 7;
this.btn_AddSon.Text = "新增子节点";
this.btn_AddSon.Click += new System.EventHandler(this.btn_AddSon_Click);
//
// btn_AddRoot
//
this.btn_AddRoot.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btn_AddRoot.BackgroundImage")));
this.btn_AddRoot.Location = new System.Drawing.Point(56, 536);
this.btn_AddRoot.Name = "btn_AddRoot";
this.btn_AddRoot.Size = new System.Drawing.Size(80, 22);
this.btn_AddRoot.TabIndex = 6;
this.btn_AddRoot.Text = "新增根节点";
this.btn_AddRoot.Click += new System.EventHandler(this.btn_AddRoot_Click);
//
// btn_Close
//
this.btn_Close.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btn_Close.BackgroundImage")));
this.btn_Close.Location = new System.Drawing.Point(632, 536);
this.btn_Close.Name = "btn_Close";
this.btn_Close.Size = new System.Drawing.Size(75, 22);
this.btn_Close.TabIndex = 11;
this.btn_Close.Text = "关 闭";
this.btn_Close.Click += new System.EventHandler(this.btn_Close_Click);
//
// SM_FunctionMaintenance
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(242)), ((System.Byte)(247)), ((System.Byte)(250)));
this.ClientSize = new System.Drawing.Size(800, 600);
this.ControlBox = false;
this.Controls.Add(this.btn_Close);
this.Controls.Add(this.btn_Help);
this.Controls.Add(this.btn_Remove);
this.Controls.Add(this.btn_Modify);
this.Controls.Add(this.btn_AddSon);
this.Controls.Add(this.btn_AddRoot);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "SM_FunctionMaintenance";
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Load += new System.EventHandler(this.SM_FunctionMaintenance_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.funcDataTable)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new SM_FunctionMaintenance(null));
}

/// <summary>
/// 查询功能表的全部内容,并初始化所有的树节点信息;把查询结果保存在funcDataTable这个DataTable对象中
/// 该过程对变量funcDataTable和treeNodes进行赋值
/// </summary>
/// <param name=""></param>
/// <returns></returns>
private void QueryAllFuncInfo()
{
funcTableConn.Open ();
OracleCommand mySelectCmd= new OracleCommand();
mySelectCmd.Connection = funcTableConn;
mySelectCmd.CommandText = "SELECT * FROM 功能 ORDER BY 功能ID" ;
mySelectCmd.CommandType = CommandType.Text ;
OracleDataAdapter myOracleDataAdapter = new OracleDataAdapter();
myOracleDataAdapter.SelectCommand=mySelectCmd;
myOracleDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
try
{
myOracleDataAdapter.Fill(funcDataTable);
}
catch(Exception ex)
{
ErrorHandle.show("查询功能表时发生异常错误。", ex, "功能维护");
//MessageBox.Show("查询功能表时出现错误:"+ex.ToString());
}
finally
{
funcTableConn.Close ( ) ;
}

}

/// <summary>
/// 查询功能ID为funcID的记录内容;把查询结构保存在funcItem这个FunctionTable对象中并且返回该结果;
/// </summary>
/// <param name="funcID"></param>
/// <returns>funcItem</returns>
private FunctionTable QueryFuncItem(long funcID)
{
int funcEntityNum=funcDataTable.Rows.Count;
int findindex=-1;
FunctionTable funcItem=new FunctionTable();
for(int i=0;i<funcEntityNum;i++)
{
if (funcID ==long.Parse((funcDataTable.Rows[i].ItemArray[0].ToString())))
{
findindex=i;
break;
}
}
if(findindex==-1)
{
ClientMessageBox.showWarn("没有查到相关的数据!", "功能维护");
//MessageBox.Show("没有查到相关的数据!");
}
else
{
funcItem.funcID=long.Parse((funcDataTable.Rows[findindex].ItemArray[0].ToString()));
funcItem.funcTag=(funcDataTable.Rows[findindex].ItemArray[1].ToString());
funcItem.funcName=(funcDataTable.Rows[findindex].ItemArray[2].ToString());
funcItem.funcDetail=(funcDataTable.Rows[findindex].ItemArray[3].ToString());
funcItem.funcBelongTo=(funcDataTable.Rows[findindex].ItemArray[4].ToString());
funcItem.funcIsInterface=(funcDataTable.Rows[findindex].ItemArray[5].ToString());
funcItem.funcType=(funcDataTable.Rows[findindex].ItemArray[6].ToString());
funcItem.funcEntity=(funcDataTable.Rows[findindex].ItemArray[7].ToString());
funcItem.funcFatherID=long.Parse((funcDataTable.Rows[findindex].ItemArray[8].ToString()));
funcItem.funcIsExpand=(funcDataTable.Rows[findindex].ItemArray[9].ToString());
funcItem.funcVer=(funcDataTable.Rows[findindex].ItemArray[10].ToString());
}
return funcItem;

}

/// <summary>
/// 查询数字字典中功能类别的内容;并把查询结构保存在ClientDictionary对象DIC_funcType中;
/// </summary>
/// <param name=""></param>
/// <returns></returns>
private void QueryFuncTypeDictionary()
{
funcTableConn.Open ();
OracleCommand mySelectCmd= new OracleCommand();
mySelectCmd.Connection = funcTableConn;
mySelectCmd.CommandText = "select * from 数据字典,属性 where 数据字典.属性ID = 属性.属性ID and 属性.属性名称 = '功能类别'";
mySelectCmd.CommandType = CommandType.Text ;
try
{
OracleDataReader myReader = mySelectCmd.ExecuteReader();
int dicCount=0;
if (myReader.HasRows)
{
while (myReader.Read())
{
dicCount++;
}
}
myReader.Close();
myReader = mySelectCmd.ExecuteReader();
DIC_funcType=new ClientDictionary[dicCount];
for(int i=0;i<dicCount;i++)
{
myReader.Read();
DIC_funcType[i]=new ClientDictionary(myReader.GetString(4),myReader.GetString(3));
}
myReader.Close();
}
catch(Exception ex)
{
MessageBox.Show("查询数据字典时出现错误:"+ex.ToString());
}
finally
{
funcTableConn.Close ( ) ;
}
}


/// <summary>
/// 将数据添加到TreeView控件中
/// </summary>
/// <param name="Nds">当前层的所有结点</param>
/// <param name="parentId">当前层的父亲结点</param>
private void InitTreeView(TreeNodeCollection Nds,string parentId)
{
//建立视图,为以后设立查询条件作准备
DataView mydataview=new DataView();
//新节点
TreeNode newNode;
//新节点的父亲节点ID
string newNodeFatherId;
//dataview的数据源
mydataview.Table=funcDataTable;
//过滤条件
mydataview.RowFilter="上级功能ID="+parentId;

foreach(DataRowView drv in mydataview)
{
newNode=new TreeNode();
newNode.Tag=drv["功能ID"].ToString();
newNode.Text=drv["功能名称"].ToString();
Nds.Add(newNode);
newNodeFatherId=drv["上级功能ID"].ToString();
//递归调用遍历当前节点的子节点
InitTreeView(newNode.Nodes,newNode.Tag.ToString ());
}
}


/// <summary>
/// 取得TreeView某一节点所有的子结点编号和子结点显示名称
/// </summary>
/// <param name="enumNodes"></param>
/// <returns></returns>
private Hashtable GetNodesValue(IEnumerator enumNodes)
{
//存储树结点信息的哈希表
Hashtable result =new Hashtable();

//当前结点
TreeNode node = null;
//当前节点的父亲结点
TreeNode father = null;

//取得所有结点的编号和显示名称
while(enumNodes.MoveNext())
{
//取得一个结点和它的父亲结点
node = (TreeNode)enumNodes.Current;
father = node.Parent;

//取得当前结点的所有儿子
TreeNodeCollection sonNodes = node.Nodes;

IEnumerator sonEnumNodes = sonNodes.GetEnumerator();
//递归取得所有儿子结点的编号和显示名称
Hashtable sonResult = GetNodesValue(sonEnumNodes);

if (sonResult.Count !=0)
{
//取得所有儿子结点信息的列举
IDictionaryEnumerator enumDic = sonResult.GetEnumerator();
while(enumDic.MoveNext())
{
//将所有儿子信息添加到存储节点信息的哈希表中
result.Add(enumDic.Key, enumDic.Value);
}
}
result.Add(node.Tag, node.Text);
}

//将保存结点信息的结果返回
return result;
}



/// <summary>
/// 选中某个节点后取得该节点的ID,并显示该ID对应的信息;
/// </summary>
private void trv_Function_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
DataView mydataview = new DataView ();
mydataview.Table= funcDataTable;
mydataview.RowFilter= "功能ID="+ e.Node.Tag.ToString();
foreach ( DataRowView editRow in mydataview)
{
this.txt_Tag.Text=editRow["功能标识"].ToString();
this.txt_Name.Text=editRow["功能名称"].ToString();
this.txt_Detail.Text=editRow["功能简述"].ToString();
this.txt_BelongTo.Text=editRow["所属窗体"].ToString();
;
this.cbo_Type.SelectedValue=editRow["功能类别"].ToString();
this.txt_FunEntity.Text=editRow["功能体"].ToString();
if (editRow["有无界面"].ToString() =="0")
{
this.ckb_IsInterface.Checked=false;
}
else
{
this.ckb_IsInterface.Checked=true;
}
}
}

/// <summary>
/// 初始化树,并把焦点定位在第一个根节点上;
/// </summary>
private void SM_FunctionMaintenance_Load(object sender, System.EventArgs e)
{
InitTreeView(trv_Function.Nodes,ROOT_NODE_VALUE);
//聚焦到树的第一个节点上
trv_Function.SelectedNode=trv_Function.Nodes[0];
trv_Function.Focus();
//折叠所有的树的节点
trv_Function.CollapseAll();
}

private void btn_AddRoot_Click(object sender, System.EventArgs e)
{
SM_AddNewFunction addRootForm=new SM_AddNewFunction(0,DIC_funcType);
//addRootForm窗口关闭时将会对addRootForm.NewFuncItem这个属性进行赋值
addRootForm.ShowDialog();
//如果返回的功能ID不为-1表示增加成功,则
//更新funcDataTable,把新增的节点加到funcDataTable中
if(addRootForm.NewFuncItem.funcID !=-1)
{
DataRow myRow=funcDataTable.NewRow();
myRow["功能ID"] = addRootForm.NewFuncItem.funcID;
myRow["功能标识"] = addRootForm.NewFuncItem.funcTag;
myRow["功能名称"] = addRootForm.NewFuncItem.funcName;
myRow["功能简述"] = addRootForm.NewFuncItem.funcDetail;
myRow["所属窗体"] = addRootForm.NewFuncItem.funcBelongTo;
myRow["有无界面"] = addRootForm.NewFuncItem.funcIsInterface;
myRow["功能类别"] = addRootForm.NewFuncItem.funcType;
myRow["功能体"] = addRootForm.NewFuncItem.funcEntity;
myRow["上级功能ID"] = addRootForm.NewFuncItem.funcFatherID;
myRow["可否展显"] = addRootForm.NewFuncItem.funcIsExpand;
myRow["版本号"] = addRootForm.NewFuncItem.funcVer;

funcDataTable.Rows.Add(myRow);

//在树的根部添加新增加的节点
TreeNode newRootNode=new TreeNode(addRootForm.NewFuncItem.funcName);
newRootNode.Tag=addRootForm.NewFuncItem.funcID.ToString();
trv_Function.Nodes.Add(newRootNode);
//聚焦到新增的节点上
trv_Function.SelectedNode=newRootNode;
}
trv_Function.Focus();
}

private void btn_AddSon_Click(object sender, System.EventArgs e)
{
if(trv_Function.SelectedNode!= null)
{
long newFuncItemFatherID=long.Parse(trv_Function.SelectedNode.Tag.ToString());
SM_AddNewFunction addSonForm=new SM_AddNewFunction(newFuncItemFatherID,DIC_funcType);
//addSonForm窗口关闭时将会对addSonForm.NewFuncItem这个属性进行赋值
addSonForm.ShowDialog();
//如果返回的功能ID不为-1表示增加成功,则
//更新funcDataTable,把新增的节点加到funcDataTable中
if(addSonForm.NewFuncItem.funcID !=-1)
{
DataRow myRow=funcDataTable.NewRow();
myRow["功能ID"] = addSonForm.NewFuncItem.funcID;
myRow["功能标识"] = addSonForm.NewFuncItem.funcTag;
myRow["功能名称"] = addSonForm.NewFuncItem.funcName;
myRow["功能简述"] = addSonForm.NewFuncItem.funcDetail;
myRow["所属窗体"] = addSonForm.NewFuncItem.funcBelongTo;
myRow["有无界面"] = addSonForm.NewFuncItem.funcIsInterface;
myRow["功能类别"] = addSonForm.NewFuncItem.funcType;
myRow["功能体"] = addSonForm.NewFuncItem.funcEntity;
myRow["上级功能ID"] = addSonForm.NewFuncItem.funcFatherID;
myRow["可否展显"] = addSonForm.NewFuncItem.funcIsExpand;
myRow["版本号"] = addSonForm.NewFuncItem.funcVer;

funcDataTable.Rows.Add(myRow);

//在树的选定的节点添加新的子节点
TreeNode newSonNode=new TreeNode(addSonForm.NewFuncItem.funcName);
newSonNode.Tag=addSonForm.NewFuncItem.funcID.ToString();
trv_Function.SelectedNode.Nodes.Add(newSonNode);
//聚焦到新增的节点上
trv_Function.SelectedNode=newSonNode;
}
trv_Function.Focus();
}
else
{
MessageBox.Show("没有选中节点!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}

}

private void btn_Modify_Click(object sender, System.EventArgs e)
{
if(trv_Function.SelectedNode!= null)
{
long editFuncItemFatherID=long.Parse(trv_Function.SelectedNode.Tag.ToString());
FunctionTable editFuncItem =QueryFuncItem(editFuncItemFatherID);

SM_EditFunction editForm=new SM_EditFunction(editFuncItem,DIC_funcType);
//editForm窗口关闭时将会对editForm.NewFuncItem和editForm.IsEdit这两个属性进行赋值
editForm.ShowDialog();
//如果用户进行了修改,则更新funcDataTable;
if(editForm.IsEdit==true)
{
DataView mydataview = new DataView ();
mydataview.Table= funcDataTable;
mydataview.RowFilter= "功能ID="+ editFuncItemFatherID.ToString();
foreach ( DataRowView editRow in mydataview)
{
editRow["功能标识"] = editForm.NewFuncItem.funcTag;
editRow["功能名称"] = editForm.NewFuncItem.funcName;
editRow["功能简述"] = editForm.NewFuncItem.funcDetail;
editRow["所属窗体"] = editForm.NewFuncItem.funcBelongTo;
editRow["有无界面"] = editForm.NewFuncItem.funcIsInterface;
editRow["功能类别"] = editForm.NewFuncItem.funcType;
editRow["功能体"] = editForm.NewFuncItem.funcEntity;
editRow["上级功能ID"] = editForm.NewFuncItem.funcFatherID;
editRow["可否展显"] = editForm.NewFuncItem.funcIsExpand;
editRow["版本号"] = editForm.NewFuncItem.funcVer;
}
//修改对应的树的节点名称
trv_Function.SelectedNode.Text=editForm.NewFuncItem.funcName;

//更改显示区域的相应的内容
this.txt_Tag.Text=editForm.NewFuncItem.funcTag;
this.txt_Name.Text=editForm.NewFuncItem.funcName;
this.txt_Detail.Text=editForm.NewFuncItem.funcDetail;
this.txt_BelongTo.Text=editForm.NewFuncItem.funcBelongTo;
;
this.cbo_Type.SelectedValue=editForm.NewFuncItem.funcType;
this.txt_FunEntity.Text=editForm.NewFuncItem.funcEntity;
if (editForm.NewFuncItem.funcIsInterface =="0")
{
this.ckb_IsInterface.Checked=false;
}
else
{
this.ckb_IsInterface.Checked=true;
}
}

trv_Function.Focus();
}
else
{
MessageBox.Show("没有选中要修改的节点!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}

}


private void btn_Remove_Click(object sender, System.EventArgs e)
{
if(trv_Function.SelectedNode!= null)
{
if (ClientMessageBox.showConfirm("你确定要删除这些数据吗?", "功能维护")== DialogResult.Yes)
//if (MessageBox.Show ("你确定要删除这些数据吗?", "删除数据",
//MessageBoxButtons.YesNo, MessageBoxIcon.Question)== DialogResult.Yes)
{
TreeNode nextFocusNode=new TreeNode();
//如果选中的是根节点(根据路径名是否等于节点名判断,两者相等的话则表示是根节点
if(trv_Function.SelectedNode.FullPath==trv_Function.SelectedNode.Text)
{
//取得第一层的节点数目
int nodesCount=trv_Function.Nodes.Count;

//如果不是同一层中的最后一个节点,则取下一个节点为聚焦节点
//根据Index判断节点的位置,Index等于节点数减一表示是最后一个节点
if(trv_Function.SelectedNode.Index !=nodesCount-1)
{
nextFocusNode=trv_Function.SelectedNode.NextVisibleNode;
}
//否则取该层第一节点为聚焦节点
else
{
nextFocusNode=trv_Function.Nodes[0];
}
}
//如果选中的不是根节点
else
{
//取得第该层的节点数目
int nodesCount=trv_Function.SelectedNode.Parent.Nodes.Count;

//如果选中的节点的父节点只有一个子节点,则删除后聚焦的节点为父节点
if(nodesCount==1)
{
nextFocusNode=trv_Function.SelectedNode.Parent;
}
//根据Index判断节点的位置,Index等于节点数减一表示是最后一个节点
//如果不是同一层中的最后一个节点,则取下一个节点为聚焦节点
else if(trv_Function.SelectedNode.Index !=nodesCount-1)
{
nextFocusNode=trv_Function.SelectedNode.NextVisibleNode;
}
//否则取该层第一节点为聚焦节点
else
{
nextFocusNode=trv_Function.SelectedNode.Parent.Nodes[0];
}
}
long deleteFuncItemID=long.Parse(trv_Function.SelectedNode.Tag.ToString());
//
TreeNodeCollection deleteNodes = trv_Function.SelectedNode.Nodes;
IEnumerator enumNodes = deleteNodes.GetEnumerator();

//调用GetNodesValue取得要删除的节点及其子节点的节点数目
Hashtable hashNodes =GetNodesValue(enumNodes);

//arrDeleteID数组保存要删除的节点及其子节点的ID;
long[] arrDeleteID=new long[hashNodes.Count+1];

//arrDeleteName数组保存要删除的节点及其子节点的名称;
string[] arrDeleteName=new string[hashNodes.Count+1];

//把要删除的节点的ID和名称作为数组的第一个元素
arrDeleteID[0]=deleteFuncItemID;
arrDeleteName[0]=trv_Function.SelectedNode.Text;

IDictionaryEnumerator enumHashNodes = hashNodes.GetEnumerator();
int index=0;
//把要删除的节点的所有子节点作为数组的其他元素
while(enumHashNodes.MoveNext())
{
arrDeleteID[index+1]=long.Parse(enumHashNodes.Key.ToString());
arrDeleteName[index+1]=enumHashNodes.Value.ToString();
index++;
}
//

for(int i=0;i<arrDeleteID.Length;i++)
{
DeleteFuncItem(arrDeleteID[i],arrDeleteName[i]);
}
Console.WriteLine();

//更新funcDataTable
for(int h=0;h<arrDeleteID.Length;h++)
{
for(int i=0;i<funcDataTable.Rows.Count;i++)
{
if (funcDataTable.Rows[i].ItemArray[0].ToString()==arrDeleteID[h].ToString())
{
funcDataTable.Rows[i].Delete();
break;
}
}
funcDataTable.AcceptChanges();
}

//删除树上的相关节点
trv_Function.Nodes.Remove(trv_Function.SelectedNode);

//聚焦到下一个所要聚焦的节点
trv_Function.SelectedNode=nextFocusNode;
trv_Function.Focus();
}
else
{
trv_Function.Focus();
return;
}
}
else
{
MessageBox.Show("没有选中要删除的节点!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}

}


/// <summary>
/// 删除某个功能,并且删除角色功能表和操作员授权表中相应的功能的记录;
/// 且把界面定制表中相应的功能名称后面加上“(该功能已收回)”字样,并对改表中
/// 相应的功能ID置为null
/// </summary>
private void DeleteFuncItem(long funcID,string funcName)
{
funcTableConn.Open();
System.Data.OracleClient.OracleTransaction myTran = funcTableConn.BeginTransaction ();
try
{
OracleCommand cmd = new OracleCommand ();
cmd.Transaction = myTran;
cmd.Connection =funcTableConn;
cmd.CommandText = "DELETE from 功能 where 功能ID = '"+funcID+"'";
cmd.ExecuteNonQuery ();
cmd.CommandText = "DELETE from 角色功能 where 功能ID = '"+funcID+"'";
cmd.ExecuteNonQuery ();
cmd.CommandText = "DELETE from 操作员授权 where 功能ID = '"+funcID+"'";
cmd.ExecuteNonQuery ();
string newFuncName=funcName+"(该功能已收回!)";
cmd.CommandText = "UPDATE 界面定制 set 功能ID=null,节点名称='" + newFuncName+"'"+ " where 功能ID ="+funcID;
cmd.ExecuteNonQuery ();
myTran.Commit ();
}
catch(Exception ee)
{
try
{
myTran.Rollback();
MessageBox.Show ("rollback over");
}
catch (OracleException ex)
{
if (myTran.Connection != null)
{
MessageBox.Show ("在回滚时发生 " + ex.GetType() +" 异常!",
"警告",System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning);
}
}

MessageBox.Show ("发生" + ee.GetType() +
"异常\n" +"删除功能出错!","警告",System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning);
}
finally
{
funcTableConn.Close();
}
}

private void btn_Help_Click(object sender, System.EventArgs e)
{
trv_Function.Nodes.Clear();
InitTreeView(trv_Function.Nodes,ROOT_NODE_VALUE);
trv_Function.ExpandAll();

}
private void btn_Close_Click(object sender, System.EventArgs e)
{
this.Close();
}


private void trv_Function_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
//开始进行"Drag"操作
DoDragDrop ((TreeNode)e.Item,DragDropEffects.Move);
}
}

private void trv_Function_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{


TreeNode temp = new TreeNode ();
//得到要移动的节点
TreeNode moveNode = (TreeNode)e.Data.GetData (temp.GetType());
//转换坐标为控件treeview的坐标
Point position=new Point(0,0);
position.X = e.X ;
position.Y = e.Y ;
position = trv_Function.PointToClient(position);

//得到移动的目的地的节点
TreeNode aimNode =trv_Function.GetNodeAt(position) ;

if (IsDragEnable(aimNode,moveNode)==true)
{
if (aimNode!=moveNode)
{
TreeNode tempNode= new TreeNode ();
trv_Function.Nodes.Remove(moveNode);
if (aimNode ==null)
{
trv_Function.Nodes.Insert(trv_Function.Nodes.Count,moveNode);
}
else
{
aimNode.Nodes.Add(moveNode);
}

//更新funcDataTable
DataView mydataview = new DataView ();
mydataview.Table= funcDataTable;
mydataview.RowFilter= "功能ID="+ moveNode.Tag.ToString()+"";
//保存要移动的节点的新的父节点ID;
string moveNodeFatherID="0";
foreach ( DataRowView editRow in mydataview)
{
if (aimNode==null)
{
//如果是根节点
moveNodeFatherID="0";
}
else
{
moveNodeFatherID=aimNode.Tag.ToString();
}
editRow["上级功能ID"]=moveNodeFatherID;
}
//聚焦到要移动的节点上
trv_Function.SelectedNode=moveNode;

//更新数据库中的功能表:改变移动节点的父节点字段为新的父节点
funcTableConn.Open();
System.Data.OracleClient.OracleTransaction myTran = funcTableConn.BeginTransaction ();
try
{
OracleCommand cmd = new OracleCommand ();
cmd.CommandText ="UPDATE 功能 set 上级功能ID='"+moveNodeFatherID +"' where 功能ID = '"+moveNode.Tag.ToString()+"'";
cmd.Transaction = myTran;
cmd.Connection =funcTableConn;
cmd.ExecuteNonQuery ();
myTran.Commit ();
}
catch(Exception ee)
{
try
{
myTran.Rollback();
}
catch (OracleException ex)
{
if (myTran.Connection != null)
{
MessageBox.Show ("在回滚时发生 " + ex.GetType() +" 异常!",
"警告",System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning);
}
}

MessageBox.Show ("发生" + ee.GetType() +
"异常\n" +"修改记录失败!","警告",System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning);
}
finally
{
funcTableConn.Close();
}
//数据库更新完毕!
}
}

}

private void trv_Function_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}

private void trv_Function_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
{
Point position=new Point(0,0);
position.X = e.X ;
position.Y = e.Y ;
position = trv_Function.PointToClient ( position ) ;
TreeNode dropNode = trv_Function.GetNodeAt ( position ) ;
trv_Function.SelectedNode=dropNode ;
trv_Function.Focus();
}

/// <summary>
/// 判断是否可以拖动动目标节点,如果可以则返回true,否则为false;
/// 判断根据是:目标节点不能是被拖动的节点的父亲节点!
/// </summary>
private bool IsDragEnable( TreeNode aimNode,TreeNode oriNode)
{
while (aimNode!=null)
{
if (aimNode.Parent !=oriNode)
{
aimNode = aimNode.Parent;
IsDragEnable( aimNode,oriNode);
}
else
{
return false;
}
}
return true;
}


}
}

posted on 2005-12-31 13:53  卓如  阅读(835)  评论(1编辑  收藏  举报