简体   繁体   中英

C# add child to Parents treeView

my database

CREATE TABLE [dbo].[tb1] (
    [name]         NVARCHAR (MAX) NOT NULL,
    [code]         INT            NOT NULL,
    [sub]          BIT            NOT NULL,
    [level]        INT            NOT NULL,
    [Sub_Lang] NVARCHAR (MAX) NOT NULL,
    PRIMARY KEY CLUSTERED ([code] ASC)
);

my data to database

name | code | sub   | level | Sub_Lang
.net | 100  | false |   0   |   0
C#   | 101  | true  |   1   |  100
VB   | 102  | true  |   1   |  100
asp  | 103  | true  |   2   |  100_101
asp  | 105  | true  |   2   |  100_102
mvc  | 104  | true  |   3   |  100_101_103
php  | 106  | false |   0   |  0

I can read from the database

DataTable dt1

for (int i = 0; i < dt1.Rows.Count; i++)
{
    treeView1.Nodes.Add(dt1.Rows[i].Field<int>("code").ToString(),
                        dt1.Rows[i].Field<string>("name").ToString());
    treeView1.Nodes[i].Tag = dt1.Rows[i].Field<int>("code").ToString();
}

But I can not show the treeView In this case

.net
    c#
       asp
       mvc
    VB
       asp
php

how I show in treeView

I'm sorry OK Edit

May be not best solution, but still give it a try.

I have used Sub_Lang column of datatable to build hierarchy of TreeView. Used Dictionary to store tree nodes. Code column is used as key.

private void LoadTreeView(DataTable dt)
    {
        var dNodes = new Dictionary<string, TreeNode>();

        foreach (DataRow dRow in dt.Rows)
        {
            string sSublang = dRow["Sub_lang"].ToString();
            string sCode = dRow["code"].ToString();
            string sName = dRow["name"].ToString();

            if (sSublang == "0")
            {
                var tn = treeView1.Nodes.Add(sCode, sName);
                dNodes.Add(sCode, tn);
            }
            else
            {
                string[] arrSubLang = sSublang.Split('_');

                for (int i = arrSubLang.Length - 1; i >= 0; i--)
                {
                    string sFindCode = arrSubLang[i];
                    var tnLastParent = default(TreeNode);

                    if (dNodes.ContainsKey(sFindCode))
                    {
                        var tn = dNodes[sFindCode];

                        if (tnLastParent != default(TreeNode))
                        {
                            tn.Nodes.Add(tnLastParent);
                            tnLastParent = tn;
                        }
                        else if (!dNodes.ContainsKey(sCode))
                        {
                            tnLastParent = tn.Nodes.Add(sCode, sName);
                            dNodes.Add(sCode, tnLastParent);
                        }
                    }
                }

            }
        }

        treeView1.ExpandAll();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM