62,050
社区成员
发帖
与我相关
我的任务
分享
TreeView右键菜单从数据库中读取,插入,删除,查找(高亮显示并
TreeView右键菜单从数据库中读取,插入,删除,查找(高亮显示并展开),编辑(即地编辑)的例子
这个项目包括二个文件,分别为From1.cs and FormCommon.cs,一个配置文件App.config
Form1.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace Tree
{
///
/// Form1 的摘要说明。
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1;
private System.Data.SqlClient.SqlConnection sqlConnection1;
///
/// 必需的设计器变量。
///
private System.ComponentModel.Container components = null;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuAdd;
private System.Windows.Forms.MenuItem menuDel;
private System.Windows.Forms.MenuItem menuEdit;
private System.Windows.Forms.MenuItem menuSearch;
private ArrayList list;
private ArrayList listnode;
private string strcomm;
private bool isselected=true;
private System.Windows.Forms.ListBox listBox1;
private TreeNode selectnode ;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
///
/// 清理所有正在使用的资源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.treeView1 = new System.Windows.Forms.TreeView();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuAdd = new System.Windows.Forms.MenuItem();
this.menuDel = new System.Windows.Forms.MenuItem();
this.menuEdit = new System.Windows.Forms.MenuItem();
this.menuSearch = new System.Windows.Forms.MenuItem();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.AllowDrop = true;
this.treeView1.ContextMenu = this.contextMenu1;
this.treeView1.HotTracking = true;
this.treeView1.ImageIndex = -1;
this.treeView1.LabelEdit = true;
this.treeView1.Location = new System.Drawing.Point(16, 16);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(200, 388);
this.treeView1.TabIndex = 0;
this.treeView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeView1_MouseDown);
this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
this.treeView1.AfterLabelEdit += new System.Windows.Forms.NodeLabelEditEventHandler(this.treeView1_AfterLabelEdit);
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuAdd,
this.menuDel,
this.menuEdit,
this.menuSearch});
//
// menuAdd
//
this.menuAdd.Index = 0;
this.menuAdd.Text = "新增";
this.menuAdd.Click += new System.EventHandler(this.menuAdd_Click);
//
// menuDel
//
this.menuDel.Index = 1;
this.menuDel.Text = "删除";
this.menuDel.Click += new System.EventHandler(this.menuDel_Click);
//
// menuEdit
//
this.menuEdit.Index = 2;
this.menuEdit.Text = "编辑";
this.menuEdit.Click += new System.EventHandler(this.menuEdit_Click);
//
// menuSearch
//
this.menuSearch.Index = 3;
this.menuSearch.Text = "查找";
this.menuSearch.Click += new System.EventHandler(this.menuSearch_Click);
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(244, 20);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(288, 376);
this.listBox1.TabIndex = 1;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(564, 422);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.treeView1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private DataSet ds;
private SqlDataAdapter sqlDataAdapter1;
private int maxnodeid;
private void Form1_Load(object sender, System.EventArgs e)
{
string strconn=ConfigurationSettings.AppSettings["ConnStr"];
sqlConnection1 = new SqlConnection(strconn);
this.sqlConnection1.Open();
//填充DataSet
this.CreateDataSet();
//从数据库中读取数据,通过递归生成树。
InitTree(this.treeView1.Nodes,"0");
}
private void CreateDataSet()
{
this.sqlDataAdapter1=new SqlDataAdapter("select * from s_menu ",this.sqlConnection1);
this.ds=new DataSet();
this.sqlDataAdapter1.Fill(ds,"tree");
}
private void InitTree(TreeNodeCollection Nds,string parentId)
{
DataView dv=new DataView();
TreeNode tmpNd;
string intId;
dv.Table=ds.Tables["tree"];
dv.RowFilter="ParentId='" + parentId + "'" ;
foreach(DataRowView drv in dv)
{
tmpNd=new TreeNode();
tmpNd.Tag=drv["NodeId"].ToString();
tmpNd.Text=drv["NodeName"].ToString();
Nds.Add(tmpNd);
intId=drv["ParentId"].ToString();
InitTree(tmpNd.Nodes,tmpNd.Tag.ToString());
}
}
//新增节点操作
private void insert(string type)
{//判断是新增树节点,还是子节点.
string strinsert="insert into s_menu values('{0}','{1}','{2}')";
string strformat="";
if(type=="sub")
strformat=string.Format(strinsert,maxnodeid.ToString(),this.selectnode.Tag.ToString(),this.strcomm);
else
strformat=string.Format(strinsert,maxnodeid.ToString(),"0",this.strcomm);
SqlCommand cmd=new SqlCommand(strformat,this.sqlConnection1);
cmd.ExecuteNonQuery();
}
//为新增节点算出最大的节点值,并以此值作为新增的节点ID值
private int GetMaxNodeid()
{
int pre=0,last=0;
DataSet maxds=new DataSet();
this.sqlDataAdapter1=new SqlDataAdapter("select nodeid from s_menu order by nodeid",this.sqlConnection1);
this.sqlDataAdapter1.Fill(maxds);
for(int i=0;i{
if(i+1{
pre=int.Parse(maxds.Tables[0].Rows[i][0].ToString());
last=int.Parse(maxds.Tables[0].Rows[i+1][0].ToString());
if(last-pre!=1)
return pre+1;
}
}
return last+1;
}
private void getallnode(TreeNode tn)
{
foreach(TreeNode node in tn.Nodes)
{
list.Add(node.Tag.ToString());
if(node.Nodes.Count>0)
{
getallnode(node);
}
}
}