用c#做的一个目录树(可新建、删除、重命名)

2021-01-21 13:13

阅读:458

标签:添加   原创   base   for   update   not   key   ash   ==   

数据库表结构:

create table tree(
nd_id integer primary key not null,
nd_parent integer,
nd_text varchar(20)
)

 

 

 

代码文件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 数据库_树结构
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //加载目录树
            gettreeview(); 
        }
        ///


        /// 向数据库传输数据
        ///

        ///
        private void connect(string str)
        {
            string sqlcon = "Data Source = (local);Database = treedb; User ID =sa; PWD = ‘‘";
            SqlConnection sqlconn = new SqlConnection(sqlcon);
            sqlconn.Open();
            SqlCommand com = new SqlCommand(str, sqlconn);
            com.ExecuteNonQuery();
            sqlconn.Close();
        }
        ///
        /// 目录树加载
        ///

        private void gettreeview()
        {
            string constr = " Data Source = (local); DataBase = treedb; User ID =sa; PWD = ‘‘";
            SqlConnection con = new SqlConnection(constr);
            con.Open();
            string sqlstr = " select * from tree";
            SqlDataAdapter sqldataadapter = new SqlDataAdapter(sqlstr, con);
            DataSet ds = new DataSet();
            sqldataadapter.Fill(ds, "tree");
            con.Close();

            // 加载根结点
            TreeNode tn = new TreeNode();
            tn.Text = "系统文件夹";
            tn.Name = "0";
            tn.Tag = "0";
            treeView1.Nodes.Add(tn);
            treeView1.SelectedNode = tn;

            // 加载子结点
            if (ds != null)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    tn = new TreeNode();
                    tn.Text = dr["nd_text"].ToString();
                    tn.Name = dr["nd_id"].ToString();
                    tn.Tag = dr["nd_parent"].ToString();
                    if (treeView1.SelectedNode != tn.Tag)
                    {
                        TreeNode[] tn_temp = treeView1.Nodes.Find(dr["nd_parent"].ToString(), true);
                        if (tn_temp.Length > 0)
                        {
                        treeView1.SelectedNode = tn_temp[0];
                        }
                    }
                    treeView1.SelectedNode.Nodes.Add(tn);
                }
            }
            //展开所有结点并且光标返回根结点
            treeView1.ExpandAll();
            treeView1.SelectedNode = treeView1.TopNode;
        }
        ///


        /// 添加结点
        ///

        ///
        ///
        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode tn = new TreeNode();
            tn.Text = "新建文件夹";
            int i = Convert.ToInt32(treeView1.SelectedNode.Name);
            treeView1.SelectedNode.Nodes.Add(tn);

            string str1, str2;
            str1 = "declare @a integer,@b integer  set @a =(select max(nd_id)+1 from tree) set @b = " + i + "";
            str2 = "insert into tree(nd_id,nd_parent,nd_text) values (@a,@b,‘新建文件夹‘) ";
            connect(str1 + str2);
           
            treeView1.Nodes.Clear();
            gettreeview(); 
        }
        ///


        /// 删除结点
        ///

        ///
        ///
        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode.IsExpanded == false)
            {
                int i = Convert.ToInt32(treeView1.SelectedNode.Name);
                treeView1.SelectedNode.Remove();

                string str1, str2;
                str1 = "declare @a integer set @a = " + i + "";
                str2 = "delete from tree where nd_id = @a";
                connect(str1 + str2);
            }
            else
            {
                MessageBox.Show("此文件夹非空,不能删除", "错误提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

        }
        ///


        /// 重命名结点
        ///

        ///
        ///
        private void renameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            treeView1.LabelEdit = true;
            treeView1.SelectedNode.BeginEdit();
        }
        ///
        /// 结点重命名后触发的事件
        ///

        ///
        ///
        private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            treeView1.SelectedNode.EndEdit(true);
            treeView1.LabelEdit = false;

            string nodename = e.Label;
            int i = Convert.ToInt32(treeView1.SelectedNode.Name);

            string str1, str2;
            str1 = "declare @a varchar(20) set @a = ‘" + nodename + "‘";
            str2 = " update tree set nd_text = @a where nd_id = " + i + "";
            connect(str1 + str2);
        }
    }
}
————————————————
版权声明:本文为CSDN博主「未知数007」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/kinwa007/article/details/2801199

用c#做的一个目录树(可新建、删除、重命名)

标签:添加   原创   base   for   update   not   key   ash   ==   

原文地址:https://www.cnblogs.com/ljs-13/p/12109112.html


评论


亲,登录后才可以留言!