欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# TreeView读取数据库简单实例

程序员文章站 2024-02-18 21:09:52
效果: 数据库: 思路: 利用for遍历,然后创建父节点,再根据父节点创建出子节点。 代码:复制代码 代码如下:using system;using sys...

效果:

C# TreeView读取数据库简单实例

数据库:

C# TreeView读取数据库简单实例

思路:

利用for遍历,然后创建父节点,再根据父节点创建出子节点。

代码:

复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.data;
using system.collections;
using system.configuration;
using system.data.sqlclient;

 

namespace treeview
{
    public partial class treeview : system.web.ui.page
    {
        string connection = configurationmanager.connectionstrings["sqlconnection"].tostring();
        protected void page_load(object sender, eventargs e)
        {
            select();
            createtree();
        }
        dataset ds = new dataset();
        public dataset datas(string sql, string tablename)
        {
            sqlconnection con = new sqlconnection(connection);
            sqldataadapter sqlda = new sqldataadapter(sql, con);
            sqlda.fill(ds, tablename);
            return ds;
        }

        //parentmenu:父节点,
        //grade:标识这是第几级节点,
        //flag:标识是不是尾结点,1是 0 不是,
        //href:指向的链接
        public void select()
        {
            string sql = "select * from treeview";
            ds = datas(sql, "test");
        }

        public void createtree()
        {
            try
            {
                datatable dt = ds.tables["test"];
                sortedlist sl = new sortedlist();
                treenode tn = new treenode();
                if (dt.rows.count <= 0)
                {
                    tn.text = "空";
                    tn.value = "";
                    tn.navigateurl = "";
                    tn.target = "";
                    tn.expanded = false;
                    tn.selectaction = treenodeselectaction.selectexpand;

                }
                else
                {
                    for (int i = 0; i < dt.rows.count; i++)
                    {
                        //创建父节点
                        if (dt.rows[i]["grade"].tostring() == "1")
                        {
                            treenode node = new treenode();
                            node.text = dt.rows[i]["menuname"].tostring();
                            node.value = dt.rows[i]["menuid"].tostring();
                            if (dt.rows[i]["flag"].tostring() == "1")
                            {
                                node.navigateurl = dt.rows[i]["href"].tostring();
                                tn.target = "_bank";
                            }
                            node.expanded = false;
                            node.selectaction = treenodeselectaction.expand;
                            sl.add(node.value, node);
                            tv_method.nodes.add(node);
                        }
                        else
                        {
                            //查找父节点
                            treenode parentnod = (treenode)sl.getbyindex(sl.indexofkey(dt.rows[i]["parentmenu"].tostring()));
                            //创建子节点
                            treenode tnchild = new treenode();
                            tnchild.text = dt.rows[i]["menuname"].tostring();
                            tnchild.value = dt.rows[i]["menuid"].tostring();
                            if (dt.rows[i]["flag"].tostring() == "1")
                            {
                                tnchild.navigateurl = dt.rows[i]["href"].tostring();
                                tnchild.target = "_bank";
                            }
                            tnchild.expanded = false;
                            tnchild.selectaction = treenodeselectaction.expand;
                            sl.add(tnchild.value, tnchild);
                            parentnod.childnodes.add(tnchild);
                        }
                    }
                }
            }
            catch (exception ex)
            {
                throw ex;
            }
        }
    }
}