0

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

1
  • You could build a treeview based on the value present in Sub_Lang column. Is it windows form application or asp.net?
    – Hassan
    Aug 20, 2014 at 4:44

1 Answer 1

0

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();
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.