none
Bind treeview to datatable

    Question

  • Hi All

    I'm a bit of a noob at WPF and an extreme noob at WPF databinding, so please bear with me.

    I have a datatable with some data in it, such as the following:

    Column1 Column2 Column3
    a a1 a1a
    a a1 a1b
    a a2 a2a
    a a2 a2b
    b b1 b1a
    b b1 b1b
    b b2 b2a
    b b2 b2b


    I want to bind it to a treeview so it's displayed something like this:

    -a


    -a1


    a1a


    a1b

    +a2
    -b


    +b1

    +b2


    I've tried working out the hierarchical datatemplate approach, but to no avail.

    I'm working in visual basic express 2008

    Can anyone help?

    Ta
    • Edited by niceprice Saturday, September 13, 2008 10:08 AM
    Saturday, September 13, 2008 9:42 AM

Answers

  • Hi,

    There're many ways to do this,  the easiest way is adding items to the TreeView recursively. However, to recursively add items to the TreeView, I would recommend you change the table structure to something like this:

    ID               int
    Parent         int    nullable
    Description   varchar



    void Window1_Loaded(object sender, RoutedEventArgs e)

            {

                DataTable dt = new DataTable("data");

                dt.Columns.Add("id", typeof(int));

                dt.Columns.Add("ParentId", typeof(int));

                dt.Columns.Add("description");

     

                dt.Rows.Add(1, null, "a");

                dt.Rows.Add(2, null, "b");

                dt.Rows.Add(3, 1, "a1");

                dt.Rows.Add(4, 1, "a2");

                dt.Rows.Add(5, 2, "b1");

                dt.Rows.Add(6, 2, "b2");

                dt.Rows.Add(7, 3, "a1a");

                dt.Rows.Add(8, 3, "a1b");

                dt.Rows.Add(9, 4, "a2a");

                dt.Rows.Add(10, 4, "a2b");

                dt.Rows.Add(11, 5, "b1a");

                dt.Rows.Add(12, 5, "b2a");

                dt.Rows.Add(13, 6, "b1b");

                dt.Rows.Add(14, 6, "b2b");

     

                //Use a DataSet to manage the data

                DataSet ds = new DataSet();

                ds.Tables.Add(dt);

     

                //add a relationship

                ds.Relations.Add("rsParentChild", ds.Tables["data"].Columns["id"],

                    ds.Tables["data"].Columns["ParentId"]);

     

                foreach (DataRow dr in ds.Tables["data"].Rows)

                {

                    if (dr["ParentId"] == DBNull.Value)

                    {

                        TreeViewItem root = new TreeViewItem();

                        root.Header = dr["description"].ToString();

                        treeView1.Items.Add(root);

                        PopulateTree(dr, root);

                    }

                }

            }

     

     

            public void PopulateTree(DataRow dr, TreeViewItem pNode)

            {

                foreach (DataRow row in dr.GetChildRows("rsParentChild"))

                {

                    TreeViewItem cChild = new TreeViewItem();

                    cChild.Header = row["description"].ToString();

                    pNode.Items.Add(cChild);

                    //Recursively build the tree

                    PopulateTree(row, cChild);

                }

            }

     

    For other approaches, please check these aricles:

    Binding a TreeView to a DataSet
    http://joshsmithonwpf.wordpress.com/2007/05/05/binding-a-treeview-to-a-dataset/

    TreeView on 2-level DataTable
    http://social.msdn.microsoft.com/forums/en-US/wpf/thread/8ec80b7d-17e8-4d66-b8b0-07c8bfea6213/

    Hierarchical Databinding in WPF
    http://blogs.msdn.com/chkoenig/archive/2008/05/24/hierarchical-databinding-in-wpf.aspx


    Best Regards,
    Zhi-Xin





    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Zhi-Xin Ye Friday, September 19, 2008 9:00 AM
    Thursday, September 18, 2008 12:43 PM

All replies

  • Hi,

    There're many ways to do this,  the easiest way is adding items to the TreeView recursively. However, to recursively add items to the TreeView, I would recommend you change the table structure to something like this:

    ID               int
    Parent         int    nullable
    Description   varchar



    void Window1_Loaded(object sender, RoutedEventArgs e)

            {

                DataTable dt = new DataTable("data");

                dt.Columns.Add("id", typeof(int));

                dt.Columns.Add("ParentId", typeof(int));

                dt.Columns.Add("description");

     

                dt.Rows.Add(1, null, "a");

                dt.Rows.Add(2, null, "b");

                dt.Rows.Add(3, 1, "a1");

                dt.Rows.Add(4, 1, "a2");

                dt.Rows.Add(5, 2, "b1");

                dt.Rows.Add(6, 2, "b2");

                dt.Rows.Add(7, 3, "a1a");

                dt.Rows.Add(8, 3, "a1b");

                dt.Rows.Add(9, 4, "a2a");

                dt.Rows.Add(10, 4, "a2b");

                dt.Rows.Add(11, 5, "b1a");

                dt.Rows.Add(12, 5, "b2a");

                dt.Rows.Add(13, 6, "b1b");

                dt.Rows.Add(14, 6, "b2b");

     

                //Use a DataSet to manage the data

                DataSet ds = new DataSet();

                ds.Tables.Add(dt);

     

                //add a relationship

                ds.Relations.Add("rsParentChild", ds.Tables["data"].Columns["id"],

                    ds.Tables["data"].Columns["ParentId"]);

     

                foreach (DataRow dr in ds.Tables["data"].Rows)

                {

                    if (dr["ParentId"] == DBNull.Value)

                    {

                        TreeViewItem root = new TreeViewItem();

                        root.Header = dr["description"].ToString();

                        treeView1.Items.Add(root);

                        PopulateTree(dr, root);

                    }

                }

            }

     

     

            public void PopulateTree(DataRow dr, TreeViewItem pNode)

            {

                foreach (DataRow row in dr.GetChildRows("rsParentChild"))

                {

                    TreeViewItem cChild = new TreeViewItem();

                    cChild.Header = row["description"].ToString();

                    pNode.Items.Add(cChild);

                    //Recursively build the tree

                    PopulateTree(row, cChild);

                }

            }

     

    For other approaches, please check these aricles:

    Binding a TreeView to a DataSet
    http://joshsmithonwpf.wordpress.com/2007/05/05/binding-a-treeview-to-a-dataset/

    TreeView on 2-level DataTable
    http://social.msdn.microsoft.com/forums/en-US/wpf/thread/8ec80b7d-17e8-4d66-b8b0-07c8bfea6213/

    Hierarchical Databinding in WPF
    http://blogs.msdn.com/chkoenig/archive/2008/05/24/hierarchical-databinding-in-wpf.aspx


    Best Regards,
    Zhi-Xin





    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Zhi-Xin Ye Friday, September 19, 2008 9:00 AM
    Thursday, September 18, 2008 12:43 PM
  • Hi Zhi,

    Many many thanks to you for you valuable reply. I am able to resolve my issue. Thanks again....

    Saturday, May 18, 2013 5:52 AM