『原创』C#中TreeView类操作全攻略:建立树,新增节点,删除节点,修改节点,拖动节点,与Oracle数据库交互

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
{
///

1<summary>   
2/// Form1 的摘要说明。   
3/// </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;

//树结点的临时信息
///

1<summary>   
2/// 必需的设计器变量。   
3/// </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;
}

///

1<summary>   
2/// 清理所有正在使用的资源。   
3/// </summary>

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
///

1<summary>   
2/// 设计器支持所需的方法 - 不要使用代码编辑器修改   
3/// 此方法的内容。   
4/// </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

///

1<summary>   
2/// 应用程序的主入口点。   
3/// </summary>

[STAThread]
static void Main()
{
Application.Run(new SM_FunctionMaintenance(null));
}

///

1<summary>   
2/// 查询功能表的全部内容,并初始化所有的树节点信息;把查询结果保存在funcDataTable这个DataTable对象中   
3/// 该过程对变量funcDataTable和treeNodes进行赋值   
4/// </summary>

///

1<param name=""/>

///

1<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 ( ) ;
}

}

///

1<summary>   
2/// 查询功能ID为funcID的记录内容;把查询结构保存在funcItem这个FunctionTable对象中并且返回该结果;   
3/// </summary>

///

1<param name="funcID"/>

///

1<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

  1<funcentitynum;i++) "功能维护");="" (funcid="long.Parse((funcDataTable.Rows[i].ItemArray[0].ToString())))" <summary="" break;="" clientmessagebox.showwarn("没有查到相关的数据!",="" else="" findindex="i;" funcitem.funcbelongto="(funcDataTable.Rows[findindex].ItemArray[4].ToString());" funcitem.funcdetail="(funcDataTable.Rows[findindex].ItemArray[3].ToString());" funcitem.funcentity="(funcDataTable.Rows[findindex].ItemArray[7].ToString());" funcitem.funcfatherid="long.Parse((funcDataTable.Rows[findindex].ItemArray[8].ToString()));" funcitem.funcid="long.Parse((funcDataTable.Rows[findindex].ItemArray[0].ToString()));" funcitem.funcisexpand="(funcDataTable.Rows[findindex].ItemArray[9].ToString());" funcitem.funcisinterface="(funcDataTable.Rows[findindex].ItemArray[5].ToString());" funcitem.funcname="(funcDataTable.Rows[findindex].ItemArray[2].ToString());" funcitem.functag="(funcDataTable.Rows[findindex].ItemArray[1].ToString());" funcitem.functype="(funcDataTable.Rows[findindex].ItemArray[6].ToString());" funcitem.funcver="(funcDataTable.Rows[findindex].ItemArray[10].ToString());" funcitem;="" if="" if(findindex="-1)" messagebox.show("没有查到相关的数据!");="" return="" {="" }="">   
  2/// 查询数字字典中功能类别的内容;并把查询结构保存在ClientDictionary对象DIC_funcType中;   
  3///    
  4/// <param name=""/>   
  5/// <returns></returns>   
  6private void QueryFuncTypeDictionary()   
  7{   
  8funcTableConn.Open ();   
  9OracleCommand mySelectCmd= new OracleCommand();   
 10mySelectCmd.Connection = funcTableConn;   
 11mySelectCmd.CommandText = "select * from 数据字典,属性 where 数据字典.属性ID = 属性.属性ID and 属性.属性名称 = '功能类别'";   
 12mySelectCmd.CommandType = CommandType.Text ;   
 13try   
 14{   
 15OracleDataReader myReader = mySelectCmd.ExecuteReader();   
 16int dicCount=0;   
 17if (myReader.HasRows)   
 18{   
 19while (myReader.Read())   
 20{   
 21dicCount++;   
 22}   
 23}   
 24myReader.Close();   
 25myReader = mySelectCmd.ExecuteReader();   
 26DIC_funcType=new ClientDictionary[dicCount];   
 27for(int i=0;i<diccount;i++) (="" )="" ;="" <summary="" catch(exception="" clientdictionary(myreader.getstring(4),myreader.getstring(3));="" dic_functype[i]="new" ex)="" finally="" functableconn.close="" messagebox.show("查询数据字典时出现错误:"+ex.tostring());="" myreader.close();="" myreader.read();="" {="" }="">   
 28/// 将数据添加到TreeView控件中   
 29///    
 30/// <param name="Nds"/>当前层的所有结点   
 31/// <param name="parentId"/>当前层的父亲结点   
 32private void InitTreeView(TreeNodeCollection Nds,string parentId)   
 33{   
 34//建立视图,为以后设立查询条件作准备   
 35DataView mydataview=new DataView();   
 36//新节点   
 37TreeNode newNode;   
 38//新节点的父亲节点ID   
 39string newNodeFatherId;   
 40//dataview的数据源   
 41mydataview.Table=funcDataTable;   
 42//过滤条件   
 43mydataview.RowFilter="上级功能ID="+parentId;   
 44  
 45foreach(DataRowView drv in mydataview)   
 46{   
 47newNode=new TreeNode();   
 48newNode.Tag=drv["功能ID"].ToString();   
 49newNode.Text=drv["功能名称"].ToString();   
 50Nds.Add(newNode);   
 51newNodeFatherId=drv["上级功能ID"].ToString();   
 52//递归调用遍历当前节点的子节点   
 53InitTreeView(newNode.Nodes,newNode.Tag.ToString ());   
 54}   
 55} 
 56
 57  
 58/// <summary>   
 59/// 取得TreeView某一节点所有的子结点编号和子结点显示名称   
 60/// </summary>   
 61/// <param name="enumNodes"/>   
 62/// <returns></returns>   
 63private Hashtable GetNodesValue(IEnumerator enumNodes)   
 64{   
 65//存储树结点信息的哈希表   
 66Hashtable result =new Hashtable(); 
 67
 68//当前结点   
 69TreeNode node = null;   
 70//当前节点的父亲结点   
 71TreeNode father = null; 
 72
 73//取得所有结点的编号和显示名称   
 74while(enumNodes.MoveNext())   
 75{   
 76//取得一个结点和它的父亲结点   
 77node = (TreeNode)enumNodes.Current;   
 78father = node.Parent; 
 79
 80//取得当前结点的所有儿子   
 81TreeNodeCollection sonNodes = node.Nodes; 
 82
 83IEnumerator sonEnumNodes = sonNodes.GetEnumerator();   
 84//递归取得所有儿子结点的编号和显示名称   
 85Hashtable sonResult = GetNodesValue(sonEnumNodes); 
 86
 87if (sonResult.Count !=0)   
 88{   
 89//取得所有儿子结点信息的列举   
 90IDictionaryEnumerator enumDic = sonResult.GetEnumerator();   
 91while(enumDic.MoveNext())   
 92{   
 93//将所有儿子信息添加到存储节点信息的哈希表中   
 94result.Add(enumDic.Key, enumDic.Value);   
 95}   
 96}   
 97result.Add(node.Tag, node.Text);   
 98} 
 99
100//将保存结点信息的结果返回   
101return result;   
102} 
103
104/// <summary>   
105/// 选中某个节点后取得该节点的ID,并显示该ID对应的信息;   
106/// </summary>   
107private void trv_Function_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)   
108{   
109DataView mydataview = new DataView ();   
110mydataview.Table= funcDataTable;   
111mydataview.RowFilter= "功能ID="+ e.Node.Tag.ToString();   
112foreach ( DataRowView editRow in mydataview)   
113{   
114this.txt_Tag.Text=editRow["功能标识"].ToString();   
115this.txt_Name.Text=editRow["功能名称"].ToString();   
116this.txt_Detail.Text=editRow["功能简述"].ToString();   
117this.txt_BelongTo.Text=editRow["所属窗体"].ToString();   
118;   
119this.cbo_Type.SelectedValue=editRow["功能类别"].ToString();   
120this.txt_FunEntity.Text=editRow["功能体"].ToString();   
121if (editRow["有无界面"].ToString() =="0")   
122{   
123this.ckb_IsInterface.Checked=false;   
124}   
125else   
126{   
127this.ckb_IsInterface.Checked=true;   
128}   
129}   
130} 
131
132/// <summary>   
133/// 初始化树,并把焦点定位在第一个根节点上;   
134/// </summary>   
135private void SM_FunctionMaintenance_Load(object sender, System.EventArgs e)   
136{   
137InitTreeView(trv_Function.Nodes,ROOT_NODE_VALUE);   
138//聚焦到树的第一个节点上   
139trv_Function.SelectedNode=trv_Function.Nodes[0];   
140trv_Function.Focus();   
141//折叠所有的树的节点   
142trv_Function.CollapseAll();   
143} 
144
145private void btn_AddRoot_Click(object sender, System.EventArgs e)   
146{   
147SM_AddNewFunction addRootForm=new SM_AddNewFunction(0,DIC_funcType);   
148//addRootForm窗口关闭时将会对addRootForm.NewFuncItem这个属性进行赋值   
149addRootForm.ShowDialog();   
150//如果返回的功能ID不为-1表示增加成功,则   
151//更新funcDataTable,把新增的节点加到funcDataTable中   
152if(addRootForm.NewFuncItem.funcID !=-1)   
153{   
154DataRow myRow=funcDataTable.NewRow();   
155myRow["功能ID"] = addRootForm.NewFuncItem.funcID;   
156myRow["功能标识"] = addRootForm.NewFuncItem.funcTag;   
157myRow["功能名称"] = addRootForm.NewFuncItem.funcName;   
158myRow["功能简述"] = addRootForm.NewFuncItem.funcDetail;   
159myRow["所属窗体"] = addRootForm.NewFuncItem.funcBelongTo;   
160myRow["有无界面"] = addRootForm.NewFuncItem.funcIsInterface;   
161myRow["功能类别"] = addRootForm.NewFuncItem.funcType;   
162myRow["功能体"] = addRootForm.NewFuncItem.funcEntity;   
163myRow["上级功能ID"] = addRootForm.NewFuncItem.funcFatherID;   
164myRow["可否展显"] = addRootForm.NewFuncItem.funcIsExpand;   
165myRow["版本号"] = addRootForm.NewFuncItem.funcVer; 
166
167funcDataTable.Rows.Add(myRow); 
168
169//在树的根部添加新增加的节点   
170TreeNode newRootNode=new TreeNode(addRootForm.NewFuncItem.funcName);   
171newRootNode.Tag=addRootForm.NewFuncItem.funcID.ToString();   
172trv_Function.Nodes.Add(newRootNode);   
173//聚焦到新增的节点上   
174trv_Function.SelectedNode=newRootNode;   
175}   
176trv_Function.Focus();   
177} 
178
179private void btn_AddSon_Click(object sender, System.EventArgs e)   
180{   
181if(trv_Function.SelectedNode!= null)   
182{   
183long newFuncItemFatherID=long.Parse(trv_Function.SelectedNode.Tag.ToString());   
184SM_AddNewFunction addSonForm=new SM_AddNewFunction(newFuncItemFatherID,DIC_funcType);   
185//addSonForm窗口关闭时将会对addSonForm.NewFuncItem这个属性进行赋值   
186addSonForm.ShowDialog();   
187//如果返回的功能ID不为-1表示增加成功,则   
188//更新funcDataTable,把新增的节点加到funcDataTable中   
189if(addSonForm.NewFuncItem.funcID !=-1)   
190{   
191DataRow myRow=funcDataTable.NewRow();   
192myRow["功能ID"] = addSonForm.NewFuncItem.funcID;   
193myRow["功能标识"] = addSonForm.NewFuncItem.funcTag;   
194myRow["功能名称"] = addSonForm.NewFuncItem.funcName;   
195myRow["功能简述"] = addSonForm.NewFuncItem.funcDetail;   
196myRow["所属窗体"] = addSonForm.NewFuncItem.funcBelongTo;   
197myRow["有无界面"] = addSonForm.NewFuncItem.funcIsInterface;   
198myRow["功能类别"] = addSonForm.NewFuncItem.funcType;   
199myRow["功能体"] = addSonForm.NewFuncItem.funcEntity;   
200myRow["上级功能ID"] = addSonForm.NewFuncItem.funcFatherID;   
201myRow["可否展显"] = addSonForm.NewFuncItem.funcIsExpand;   
202myRow["版本号"] = addSonForm.NewFuncItem.funcVer; 
203
204funcDataTable.Rows.Add(myRow); 
205
206//在树的选定的节点添加新的子节点   
207TreeNode newSonNode=new TreeNode(addSonForm.NewFuncItem.funcName);   
208newSonNode.Tag=addSonForm.NewFuncItem.funcID.ToString();   
209trv_Function.SelectedNode.Nodes.Add(newSonNode);   
210//聚焦到新增的节点上   
211trv_Function.SelectedNode=newSonNode;   
212}   
213trv_Function.Focus();   
214}   
215else   
216{   
217MessageBox.Show("没有选中节点!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);   
218}   
219  
220} 
221
222private void btn_Modify_Click(object sender, System.EventArgs e)   
223{   
224if(trv_Function.SelectedNode!= null)   
225{   
226long editFuncItemFatherID=long.Parse(trv_Function.SelectedNode.Tag.ToString());   
227FunctionTable editFuncItem =QueryFuncItem(editFuncItemFatherID); 
228
229SM_EditFunction editForm=new SM_EditFunction(editFuncItem,DIC_funcType);   
230//editForm窗口关闭时将会对editForm.NewFuncItem和editForm.IsEdit这两个属性进行赋值   
231editForm.ShowDialog();   
232//如果用户进行了修改,则更新funcDataTable;   
233if(editForm.IsEdit==true)   
234{   
235DataView mydataview = new DataView ();   
236mydataview.Table= funcDataTable;   
237mydataview.RowFilter= "功能ID="+ editFuncItemFatherID.ToString();   
238foreach ( DataRowView editRow in mydataview)   
239{   
240editRow["功能标识"] = editForm.NewFuncItem.funcTag;   
241editRow["功能名称"] = editForm.NewFuncItem.funcName;   
242editRow["功能简述"] = editForm.NewFuncItem.funcDetail;   
243editRow["所属窗体"] = editForm.NewFuncItem.funcBelongTo;   
244editRow["有无界面"] = editForm.NewFuncItem.funcIsInterface;   
245editRow["功能类别"] = editForm.NewFuncItem.funcType;   
246editRow["功能体"] = editForm.NewFuncItem.funcEntity;   
247editRow["上级功能ID"] = editForm.NewFuncItem.funcFatherID;   
248editRow["可否展显"] = editForm.NewFuncItem.funcIsExpand;   
249editRow["版本号"] = editForm.NewFuncItem.funcVer;   
250}   
251//修改对应的树的节点名称   
252trv_Function.SelectedNode.Text=editForm.NewFuncItem.funcName; 
253
254//更改显示区域的相应的内容   
255this.txt_Tag.Text=editForm.NewFuncItem.funcTag;   
256this.txt_Name.Text=editForm.NewFuncItem.funcName;   
257this.txt_Detail.Text=editForm.NewFuncItem.funcDetail;   
258this.txt_BelongTo.Text=editForm.NewFuncItem.funcBelongTo;   
259;   
260this.cbo_Type.SelectedValue=editForm.NewFuncItem.funcType;   
261this.txt_FunEntity.Text=editForm.NewFuncItem.funcEntity;   
262if (editForm.NewFuncItem.funcIsInterface =="0")   
263{   
264this.ckb_IsInterface.Checked=false;   
265}   
266else   
267{   
268this.ckb_IsInterface.Checked=true;   
269}   
270} 
271
272trv_Function.Focus();   
273}   
274else   
275{   
276MessageBox.Show("没有选中要修改的节点!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);   
277}   
278  
279} 
280
281  
282private void btn_Remove_Click(object sender, System.EventArgs e)   
283{   
284if(trv_Function.SelectedNode!= null)   
285{   
286if (ClientMessageBox.showConfirm("你确定要删除这些数据吗?", "功能维护")== DialogResult.Yes)   
287//if (MessageBox.Show ("你确定要删除这些数据吗?", "删除数据",   
288//MessageBoxButtons.YesNo, MessageBoxIcon.Question)== DialogResult.Yes)   
289{   
290TreeNode nextFocusNode=new TreeNode();   
291//如果选中的是根节点(根据路径名是否等于节点名判断,两者相等的话则表示是根节点   
292if(trv_Function.SelectedNode.FullPath==trv_Function.SelectedNode.Text)   
293{   
294//取得第一层的节点数目   
295int nodesCount=trv_Function.Nodes.Count; 
296
297//如果不是同一层中的最后一个节点,则取下一个节点为聚焦节点   
298//根据Index判断节点的位置,Index等于节点数减一表示是最后一个节点   
299if(trv_Function.SelectedNode.Index !=nodesCount-1)   
300{   
301nextFocusNode=trv_Function.SelectedNode.NextVisibleNode;   
302}   
303//否则取该层第一节点为聚焦节点   
304else   
305{   
306nextFocusNode=trv_Function.Nodes[0];   
307}   
308}   
309//如果选中的不是根节点   
310else   
311{   
312//取得第该层的节点数目   
313int nodesCount=trv_Function.SelectedNode.Parent.Nodes.Count;   
314  
315//如果选中的节点的父节点只有一个子节点,则删除后聚焦的节点为父节点   
316if(nodesCount==1)   
317{   
318nextFocusNode=trv_Function.SelectedNode.Parent;   
319}   
320//根据Index判断节点的位置,Index等于节点数减一表示是最后一个节点   
321//如果不是同一层中的最后一个节点,则取下一个节点为聚焦节点   
322else if(trv_Function.SelectedNode.Index !=nodesCount-1)   
323{   
324nextFocusNode=trv_Function.SelectedNode.NextVisibleNode;   
325}   
326//否则取该层第一节点为聚焦节点   
327else   
328{   
329nextFocusNode=trv_Function.SelectedNode.Parent.Nodes[0];   
330}   
331}   
332long deleteFuncItemID=long.Parse(trv_Function.SelectedNode.Tag.ToString());   
333//   
334TreeNodeCollection deleteNodes = trv_Function.SelectedNode.Nodes;   
335IEnumerator enumNodes = deleteNodes.GetEnumerator(); 
336
337//调用GetNodesValue取得要删除的节点及其子节点的节点数目   
338Hashtable hashNodes =GetNodesValue(enumNodes); 
339
340//arrDeleteID数组保存要删除的节点及其子节点的ID;   
341long[] arrDeleteID=new long[hashNodes.Count+1]; 
342
343//arrDeleteName数组保存要删除的节点及其子节点的名称;   
344string[] arrDeleteName=new string[hashNodes.Count+1]; 
345
346//把要删除的节点的ID和名称作为数组的第一个元素   
347arrDeleteID[0]=deleteFuncItemID;   
348arrDeleteName[0]=trv_Function.SelectedNode.Text; 
349
350IDictionaryEnumerator enumHashNodes = hashNodes.GetEnumerator();   
351int index=0;   
352//把要删除的节点的所有子节点作为数组的其他元素   
353while(enumHashNodes.MoveNext())   
354{   
355arrDeleteID[index+1]=long.Parse(enumHashNodes.Key.ToString());   
356arrDeleteName[index+1]=enumHashNodes.Value.ToString();   
357index++;   
358}   
359//   
360  
361for(int i=0;i<arrdeleteid.length;i++) (funcdatatable.rows[i].itemarray[0].tostring()="arrDeleteID[h].ToString())" <summary="" break;="" console.writeline();="" deletefuncitem(arrdeleteid[i],arrdeletename[i]);="" else="" for(int="" funcdatatable.acceptchanges();="" funcdatatable.rows[i].delete();="" h="0;h&lt;arrDeleteID.Length;h++)" i="0;i&lt;funcDataTable.Rows.Count;i++)" if="" messagebox.show("没有选中要删除的节点!","警告",messageboxbuttons.ok,messageboxicon.warning);="" return;="" trv_function.focus();="" trv_function.nodes.remove(trv_function.selectednode);="" trv_function.selectednode="nextFocusNode;" {="" }="" 删除树上的相关节点="" 更新funcdatatable="" 聚焦到下一个所要聚焦的节点="">   
362/// 删除某个功能,并且删除角色功能表和操作员授权表中相应的功能的记录;   
363/// 且把界面定制表中相应的功能名称后面加上“(该功能已收回)”字样,并对改表中   
364/// 相应的功能ID置为null   
365///    
366private void DeleteFuncItem(long funcID,string funcName)   
367{   
368funcTableConn.Open();   
369System.Data.OracleClient.OracleTransaction myTran = funcTableConn.BeginTransaction ();   
370try   
371{   
372OracleCommand cmd = new OracleCommand ();   
373cmd.Transaction = myTran;   
374cmd.Connection =funcTableConn;   
375cmd.CommandText = "DELETE from 功能 where 功能ID = '"+funcID+"'";   
376cmd.ExecuteNonQuery ();   
377cmd.CommandText = "DELETE from 角色功能 where 功能ID = '"+funcID+"'";   
378cmd.ExecuteNonQuery ();   
379cmd.CommandText = "DELETE from 操作员授权 where 功能ID = '"+funcID+"'";   
380cmd.ExecuteNonQuery ();   
381string newFuncName=funcName+"(该功能已收回!)";   
382cmd.CommandText = "UPDATE 界面定制 set 功能ID=null,节点名称='" + newFuncName+"'"+ " where 功能ID ="+funcID;   
383cmd.ExecuteNonQuery ();   
384myTran.Commit ();   
385}   
386catch(Exception ee)   
387{   
388try   
389{   
390myTran.Rollback();   
391MessageBox.Show ("rollback over");   
392}   
393catch (OracleException ex)   
394{   
395if (myTran.Connection != null)   
396{   
397MessageBox.Show ("在回滚时发生 " + ex.GetType() +" 异常!",   
398"警告",System.Windows.Forms.MessageBoxButtons.OK,   
399System.Windows.Forms.MessageBoxIcon.Warning);   
400}   
401}   
402  
403MessageBox.Show ("发生" + ee.GetType() +   
404"异常\n" +"删除功能出错!","警告",System.Windows.Forms.MessageBoxButtons.OK,   
405System.Windows.Forms.MessageBoxIcon.Warning);   
406}   
407finally   
408{   
409funcTableConn.Close();   
410}   
411} 
412
413private void btn_Help_Click(object sender, System.EventArgs e)   
414{   
415trv_Function.Nodes.Clear();   
416InitTreeView(trv_Function.Nodes,ROOT_NODE_VALUE);   
417trv_Function.ExpandAll(); 
418
419}   
420private void btn_Close_Click(object sender, System.EventArgs e)   
421{   
422this.Close();   
423} 
424
425  
426private void trv_Function_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)   
427{   
428if(e.Button == MouseButtons.Left)   
429{   
430//开始进行"Drag"操作   
431DoDragDrop ((TreeNode)e.Item,DragDropEffects.Move);   
432}   
433} 
434
435private void trv_Function_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)   
436{   
437  
438  
439TreeNode temp = new TreeNode ();   
440//得到要移动的节点   
441TreeNode moveNode = (TreeNode)e.Data.GetData (temp.GetType());   
442//转换坐标为控件treeview的坐标   
443Point position=new Point(0,0);   
444position.X = e.X ;   
445position.Y = e.Y ;   
446position = trv_Function.PointToClient(position);   
447  
448//得到移动的目的地的节点   
449TreeNode aimNode =trv_Function.GetNodeAt(position) ; 
450
451if (IsDragEnable(aimNode,moveNode)==true)   
452{   
453if (aimNode!=moveNode)   
454{   
455TreeNode tempNode= new TreeNode ();   
456trv_Function.Nodes.Remove(moveNode);   
457if (aimNode ==null)   
458{   
459trv_Function.Nodes.Insert(trv_Function.Nodes.Count,moveNode);   
460}   
461else   
462{   
463aimNode.Nodes.Add(moveNode);   
464} 
465
466//更新funcDataTable   
467DataView mydataview = new DataView ();   
468mydataview.Table= funcDataTable;   
469mydataview.RowFilter= "功能ID="+ moveNode.Tag.ToString()+"";   
470//保存要移动的节点的新的父节点ID;   
471string moveNodeFatherID="0";   
472foreach ( DataRowView editRow in mydataview)   
473{   
474if (aimNode==null)   
475{   
476//如果是根节点   
477moveNodeFatherID="0";   
478}   
479else   
480{   
481moveNodeFatherID=aimNode.Tag.ToString();   
482}   
483editRow["上级功能ID"]=moveNodeFatherID;   
484}   
485//聚焦到要移动的节点上   
486trv_Function.SelectedNode=moveNode; 
487
488//更新数据库中的功能表:改变移动节点的父节点字段为新的父节点   
489funcTableConn.Open();   
490System.Data.OracleClient.OracleTransaction myTran = funcTableConn.BeginTransaction ();   
491try   
492{   
493OracleCommand cmd = new OracleCommand ();   
494cmd.CommandText ="UPDATE 功能 set 上级功能ID='"+moveNodeFatherID +"' where 功能ID = '"+moveNode.Tag.ToString()+"'";   
495cmd.Transaction = myTran;   
496cmd.Connection =funcTableConn;   
497cmd.ExecuteNonQuery ();   
498myTran.Commit ();   
499}   
500catch(Exception ee)   
501{   
502try   
503{   
504myTran.Rollback();   
505}   
506catch (OracleException ex)   
507{   
508if (myTran.Connection != null)   
509{   
510MessageBox.Show ("在回滚时发生 " + ex.GetType() +" 异常!",   
511"警告",System.Windows.Forms.MessageBoxButtons.OK,   
512System.Windows.Forms.MessageBoxIcon.Warning);   
513}   
514}   
515  
516MessageBox.Show ("发生" + ee.GetType() +   
517"异常\n" +"修改记录失败!","警告",System.Windows.Forms.MessageBoxButtons.OK,   
518System.Windows.Forms.MessageBoxIcon.Warning);   
519}   
520finally   
521{   
522funcTableConn.Close();   
523}   
524//</arrdeleteid.length;i++)></diccount;i++)></funcentitynum;i++)>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus