Home > Computer Programming, dynamics ax 2009 > How to Bind DataTable into Treeview

How to Bind DataTable into Treeview


How to Bind Datatable into Treeview using C#,

I am using visual c# express, and there was a case, I have a table like this:

I Only have 2 Column, that is ParentID and CustChannelID

ParentID Column is a parent from CustChannelID Column

and What I wanted is create a treeview like this one

and this is my method

        private void frmDODaily_Load(object sender, EventArgs e)
        {
            //I defined the function of Initialization Tree
            treeV();
        }
        private void treeV()
        {
            //Get Class to get database
            csDailyDO dodaily = new csDailyDO();
           //Define the tree
            TreeNode tree = new TreeNode();
            //Get Datatable, you should know how to get one from your database
            DataTable data = dodaily.ChannelTree(“MTL_”);
            //Initial for first tree
            tree = createnode(tree, data, “LOCAL”);
            tree.Text = “LOCAL”;
            treeView1.CheckBoxes = true;
            treeView1.Nodes.Add(tree);
        }
//and this is my method, please shared to anyone
        private TreeNode createnode(TreeNode parent, DataTable data, string LatestKey)
        {
            TreeNode result = new TreeNode();
            TreeNode temp = new TreeNode();
            foreach (DataRow row in data.Rows)
            {
                if (row[“ParentID”].ToString() == LatestKey)
                {
                    temp = createnode(parent, data, row[“CUSTCHANNELID”].ToString());
                    temp.Text = row[“CUSTCHANNELID”].ToString();
                    result.Nodes.Add(temp);
                }
            }
            return result;
        }

Please Comment and shares this codes

  1. No comments yet.
  1. No trackbacks yet.

Please kindly Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: