当前位置:编程学习 > C#/ASP.NET >>

treeview控件中的节点怎么保存

treeview控件中的节点怎么保存,每次保存完之后再打开程序就发现不见了,请问有没有具体代码,详细点,谢谢.求,急 --------------------编程问答-------------------- treeview 只是显示控件 不会给你存数据   你新建的节点必须持久化数据库或本地文件中 
等 你下次再打开时又去读数据库或文件 再把节点加载到树上! --------------------编程问答--------------------
引用 1 楼 zjx198934 的回复:
treeview 只是显示控件 不会给你存数据 你新建的节点必须持久化数据库或本地文件中 
等 你下次再打开时又去读数据库或文件 再把节点加载到树上!



你好,我可以怎么去做,可以有代码贴出来吗?我是新手啦,谢谢 --------------------编程问答-------------------- 逐级逐节点的保存,每一级要有个表示,比如缩进 --------------------编程问答-------------------- 可以保存成xml文件,那样读取时,节点层次关系好控制 --------------------编程问答--------------------
引用 4 楼 bdmh 的回复:
可以保存成xml文件,那样读取时,节点层次关系好控制

只能说,我没有办法做啊,求代码啊,谢谢 --------------------编程问答-------------------- 最简单的 自己添加几个节点 应该不会不见吧 动态的就是代码生成节点 然后绑定到数据库 --------------------编程问答-------------------- 一棵树的全部源码:
请参考:
http://topic.csdn.net/u/20110224/15/b8f65ab8-bc09-400c-a82e-1534f657b292.html



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 testcheckbox
{
    public partial class treeview : Form
    {
        public treeview()
        {
            InitializeComponent();
        }

        //定义数据集,将树信息放入DataTable中
        DataSet ds = new DataSet();

        //树节点是否完全展开
        private bool isexpand=false;

        //选中的节点的id
        private int nodeid = -1;

        //数据库连接字符串
        private string connstr="data source=127.1;initial catalog=test; user id=sa; password=sa;";
        private void treeview_Load(object sender, EventArgs e)
        {
            DateTime s = DateTime.Now;
            tv_bind();
            toolStripStatusLabel1.Text ="构造树耗时:"+DateTime.Now.Subtract(s).TotalMilliseconds+"毫秒";
        }
        //将数据库中的分类信息放入数据表中
        private void tv_bind()
        {
            try
            {
                //从数据库中读取分类信息至DataSet
                using (SqlConnection conn = new SqlConnection(connstr))
                {
                    using (SqlDataAdapter da = new SqlDataAdapter("select * from bumen", conn))
                    {
                        da.Fill(ds,"bumen");
                    }
                }
                //如果数据库中有信息那么构造树,否则提示无接点
                if (ds.Tables["bumen"].Rows.Count > 0)
                {
                    //清除树节点,从根添加树
                    treeView1.Nodes.Clear();
                    Add_tree(0,(TreeNode)null);
                }
                else
                {
                    treeView1.Nodes.Clear();
                    TreeNode node = new TreeNode();
                    node.Text = "暂无任何分类信息";
                    treeView1.Nodes.Add(node);
                }
            }
            catch
            {
                treeView1.Nodes.Clear();
                TreeNode node = new TreeNode();
                node.Text = "暂无任何分类信息:请检查数据库连接是否正常";
                treeView1.Nodes.Add(node);
            }

        }

        //递归树
        private void Add_tree(int pid,TreeNode pnode)
        {
            DataRow[] dr = ds.Tables["bumen"].Select("pid="+pid);
            foreach (DataRow row in dr)
            {
                TreeNode node = new TreeNode();
                if (pnode == null)
                {
                    node.Text = row["bumen"].ToString();
                    node.Tag = row["id"].ToString();
                    treeView1.Nodes.Add(node);
                    Add_tree(int.Parse(row["id"].ToString()),node);
                }
                else
                {
                    node.Text = row["bumen"].ToString();
                    node.Tag = row["id"].ToString();
                    pnode.Nodes.Add(node);
                    Add_tree(int.Parse(row["id"].ToString()),node);
                }
            }
        }

        private void toolStripLabel1_Click(object sender, EventArgs e)
        {
            if (isexpand)
            {
                isexpand = false;
                treeView1.CollapseAll();
            }
            else
            {
                isexpand = true;
                treeView1.ExpandAll();
            }
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                //选中右键点击的节点
                treeView1.SelectedNode = e.Node;
                //显示右键菜单
                contextMenuStrip1.Visible = true;
                //设置右键菜单的位置
                contextMenuStrip1.Left = MousePosition.X;
                contextMenuStrip1.Top = MousePosition.Y;
            }
            else
            {
                //点击其他节点时,取消正在编辑状态的节点
                textBox1.Text = "";
                groupBox1.Enabled = false;
                //显示当前选择的节点的信息
                richTextBox1.Text = "名称:"+e.Node.Text+"\n节点属性:"+e.Node.Tag;

                //根据点击的节点到yuangong表提取对应的员工,然后在listview中显示
                listView1.Clear();
                using (SqlConnection conn = new SqlConnection(connstr))
                {
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand("select yuangong from yuangong where bid=" + e.Node.Tag, conn))
                    {
                        SqlDataReader dr = cmd.ExecuteReader();
                        while (dr.Read())
                        {
                            listView1.Items.Add(dr[0].ToString());
                        }
                        dr.Close();
                    }
                }
            }
        }

        private void 编辑节点ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (int.Parse(treeView1.SelectedNode.Tag.ToString()) >-1)
            {
                nodeid = int.Parse(treeView1.SelectedNode.Tag.ToString());
                groupBox1.Enabled = true;
                textBox1.Text = treeView1.SelectedNode.Text;
            }
            else
            {
                MessageBox.Show("请选择要编辑的节点");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (nodeid > -1)
            { 
                //检查同级别节点是否有重名
                using (SqlConnection conn = new SqlConnection("data source=127.1;initial catalog=test;user id=sa;password=sa;"))
                {
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand("select count(id) from bumen where pid=(select pid from bumen where id=" + nodeid + ") and bumen='" + textBox1.Text.Trim() + "' and id<>"+nodeid, conn))
                    {
                        int i = int.Parse(cmd.ExecuteScalar().ToString());
                        if (i > 0)
                        {
                            MessageBox.Show("错误!该分类已经存在同名节点");
                        }
                        else
                        {
                            cmd.CommandText = "update bumen set bumen='"+textBox1.Text.Trim()+"' where id="+nodeid;
                            int isupdateok = cmd.ExecuteNonQuery();
                            if (isupdateok == 1)
                            {
                                MessageBox.Show("编辑节点信息成功!");
                                textBox1.Text = "";
                                groupBox1.Enabled = false;
                                treeView1.Nodes.Clear();
                                tv_bind();
                                treeView1.ExpandAll();
                            }
                            else
                            {
                                MessageBox.Show("编辑节点信息失败!请重新编辑","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
                            }
                        }
                    }
                }

            }
        }
    }
}




如果想要源码给我留下邮箱地址,我给你发一个 --------------------编程问答-------------------- TreeNode node = new TreeNode("node");

this.treeview1.Nodes.add(node); --------------------编程问答-------------------- 节点的数据要写入到数据源中,然后界面显示的时候再读出来! --------------------编程问答-------------------- private void tvType_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {

            if (e.Node.Level == 0 || string.IsNullOrEmpty(e.Label) ||
                this.tvManager.FindNameInTable(e.Label))
            {
                e.CancelEdit = true;
                return;
            }
            this.tvManager.ChangeNodeValue(e.Node.Tag.ToString(), e.Label);  // 更改值
        }
http://www.360doc.com/content/10/1218/16/4480089_79281385.shtml --------------------编程问答-------------------- --------------------编程问答-------------------- 完善了一下我的树,增加了添加节点功能(没注意以前写的只能编辑不能添加,不好意思啊呵呵)

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

namespace myTreeView
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //定义数据集,将树信息放入DataTable中
        DataSet ds = new DataSet();
        //树节点是否完全展开
        private bool isexpand = false;
        //选中的节点的id
        private int nodeid = -1;
        private TreeNode lastnode;
        //定义数据库的连接字符串
        private string connstr = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=db.mdb;";


        private void Form1_Load(object sender, EventArgs e)
        {
            tv_bind();
        }

        private void tv_bind()
        {
            try
            {
                //从数据库中读取分类信息至DataSet
                //绑定前先清空数据集
                ds.Clear();
                treeView1.Nodes.Clear();
                using (OleDbConnection conn = new OleDbConnection(connstr))
                {
                    using (OleDbDataAdapter da = new OleDbDataAdapter("select * from tree", conn))
                    {
                        da.Fill(ds, "tree");
                    }
                }
                //如果数据库中有信息那么构造树,否则提示无接点
                if (ds.Tables["tree"].Rows.Count > 0)
                {
                    //清除树节点,从根添加树
                    treeView1.Nodes.Clear();
                    Add_tree(0, (TreeNode)null);
                }
                else
                {
                    treeView1.Nodes.Clear();
                    TreeNode node = new TreeNode();
                    node.Text = "暂无任何分类信息";
                    treeView1.Nodes.Add(node);
                }
            }
            catch
            {
                treeView1.Nodes.Clear();
                TreeNode node = new TreeNode();
                node.Text = "暂无任何分类信息:请检查数据库连接是否正常";
                treeView1.Nodes.Add(node);
            }

        }

        //递归树
        private void Add_tree(int pid, TreeNode pnode)
        {
            DataRow[] dr = ds.Tables["tree"].Select("pid=" + pid);
            foreach (DataRow row in dr)
            {
                TreeNode node = new TreeNode();
                if (pnode == null)
                {
                    node.Text = row["nodename"].ToString();
                    node.Tag = row["id"].ToString();
                    treeView1.Nodes.Add(node);
                    Add_tree(int.Parse(row["id"].ToString()), node);
                }
                else
                {
                    node.Text = row["nodename"].ToString();
                    node.Tag = row["id"].ToString();
                    pnode.Nodes.Add(node);
                    Add_tree(int.Parse(row["id"].ToString()), node);
                }
            }
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            //如果在节点上点击的是左键
            if (e.Button == MouseButtons.Left)
            {
                //根据点击的节点到yuangong表提取对应的员工,然后在listview中显示
                listView1.Clear();
                using (OleDbConnection conn = new OleDbConnection(connstr))
                {
                    conn.Open();
                    using (OleDbCommand cmd = new OleDbCommand("select products_name from products where tid=" + e.Node.Tag, conn))
                    {
                        OleDbDataReader dr = cmd.ExecuteReader();
                        while (dr.Read())
                        {
                            listView1.Items.Add(dr[0].ToString());
                        }
                        dr.Close();
                    }
                }
            }
            //如果在节点上点击鼠标右键
            else
            {
                treeView1.SelectedNode = e.Node;
                contextMenuStrip1.Visible = true;
                contextMenuStrip1.Left = MousePosition.X;
                contextMenuStrip1.Top = MousePosition.Y;
            }
        }

        private void 折叠展开树ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (isexpand)
            {
                isexpand = false;
                treeView1.CollapseAll();
            }
            else
            {
                isexpand = true;
                treeView1.ExpandAll();
            }

        }

        private void 编辑节点ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (int.Parse(treeView1.SelectedNode.Tag.ToString()) > -1)
            {
                nodeid = int.Parse(treeView1.SelectedNode.Tag.ToString());
                lastnode = treeView1.SelectedNode;
                groupBox1.Enabled = true;
                textBox1.Text = treeView1.SelectedNode.Text;
                button1.Text = "编辑";
            }
            else
            {
                MessageBox.Show("请选择要编辑的节点");
            }
        }
        private void 添加子节点ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (int.Parse(treeView1.SelectedNode.Tag.ToString()) > -1)
            {
                nodeid = int.Parse(treeView1.SelectedNode.Tag.ToString());
                groupBox1.Enabled = true;
                button1.Text = "添加";
            }
            else
            {
                MessageBox.Show("请选择要在哪个节点下添加子节点");
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //判断是编辑节点还是添加节点
            if (button1.Text == "编辑"&&textBox1.Text.Trim().Length>0)
            {
                if (nodeid > -1)
                {
                    //检查同级别节点是否有重名
                    using (OleDbConnection conn = new OleDbConnection(connstr))
                    {
                        conn.Open();
                        using (OleDbCommand cmd = new OleDbCommand("select count(id) from tree where pid=(select pid from tree where id=" + nodeid + ") and nodename='" + textBox1.Text.Trim() + "' and id<>" + nodeid, conn))
                        {
                            int i = int.Parse(cmd.ExecuteScalar().ToString());
                            if (i > 0)
                            {
                                MessageBox.Show("错误!该分类已经存在同名节点");
                            }
                            else
                            {
                                cmd.CommandText = "update tree set nodename='" + textBox1.Text.Trim() + "' where id=" + nodeid;
                                int isupdateok = cmd.ExecuteNonQuery();
                                if (isupdateok == 1)
                                {
                                    MessageBox.Show("编辑节点信息成功!");
                                    textBox1.Text = "";
                                    groupBox1.Enabled = false;
                                    tv_bind();
                                }
                                else
                                {
                                    MessageBox.Show("编辑节点信息失败!请重新编辑", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }
                            }
                        }
                    }
                }
            }
            else if (button1.Text == "添加")
            {
                if (textBox1.Text.Trim().Length > 0)
                {
                    using (OleDbConnection conn = new OleDbConnection(connstr))
                    {
                        conn.Open();
                        string addsql = "select count(id) From tree where pid=" + nodeid + " and nodename='" + textBox1.Text.Trim()+"'";
                        using (OleDbCommand addcmd = new OleDbCommand(addsql, conn))
                        {
                            int iscanadd = int.Parse(addcmd.ExecuteScalar().ToString());
                            if (iscanadd > 0)
                            {
                                MessageBox.Show("对不起,已存在同名节点");
                            }
                            else
                            {
                                string intsql = "Insert into tree(pid,nodename) values(" + nodeid + ",'" + textBox1.Text.Trim() + "')";
                                using (OleDbCommand cmd = new OleDbCommand(intsql, conn))
                                {
                                    int isaddok = cmd.ExecuteNonQuery();
                                    if (isaddok > 0)
                                    {
                                        MessageBox.Show("添加节点成功!");
                                        tv_bind();
                                        treeView1.ExpandAll();
                                    }
                                    else
                                    {
                                        MessageBox.Show("添加节点失败,请重新尝试。");
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    MessageBox.Show("节点名称不能为空!");
                }
            }
            else
            {
                MessageBox.Show("请从右键菜单选择您要进行的操作");
            }
        }

    }
}




自己测试吧,需要源文件留下邮箱
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,