ASP.NET Tree View out of Sql Binding

I got a Categorie Table with a column for the Tree View in my Database which I want to display as a Tree View.

My TreeView Column Looks like the following. (I Hope you understand)

0001.
0001.0001.
0001.0002.
0001.0002.0001.
0001.0002.0002.
0001.0003.
0002.
...
...
...

Is this possible to be displayed in a asp:TreeView? Or what other way can I use to display this?

The Database already existed I’m just designing the website & can’t do much in the database.

EDIT:

I tried the first Solutions and now I got this.
Image

But I don’t want to display the number, just the name and the number just as value invisible for me to use when I click on it.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Try something like following :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication64
{
    public partial class Form1 : Form
    {
        DataTable dt = null;
        public Form1()
        {
            InitializeComponent();

            dt = new DataTable();
            dt.Columns.Add("Index", typeof(string));
            dt.Columns.Add("Value", typeof(string));

            dt.Rows.Add(new object[] { "0001.", "aaaa"});
            dt.Rows.Add(new object[] { "0001.0001.", "bbbb"});
            dt.Rows.Add(new object[] { "0001.0002.", "cccc"});
            dt.Rows.Add(new object[] { "0001.0002.0001.", "dddd"});
            dt.Rows.Add(new object[] { "0001.0002.0002.", "eeee"});
            dt.Rows.Add(new object[] { "0001.0003.", "ffff"});
            dt.Rows.Add(new object[] { "0002.", "gggg" });

            new Index(dt);

            MakeTreeRecursive(0, Index.indexes, null);
            treeView1.ExpandAll();

        }
        public void MakeTreeRecursive(int level, List<Index> rows, TreeNode parent)
        {
            TreeNode child = null;

            var groups = rows
                .OrderBy(x => x.paragraphs[level])
                .GroupBy(x => x.paragraphs[level]);

            foreach (var group in groups)
            {
                child = new TreeNode(group.Key);
                if (parent == null)
                {
                    treeView1.Nodes.Add(child);
                }
                else
                {
                    parent.Nodes.Add(child);
                }

                foreach (Index node in group.Where(x => x.paragraphs.Count == level + 1))
                {
                    child.Nodes.Add(node.row.Field<string>("Value"));
                }
                List<Index> descendants = group.Where(x => x.paragraphs.Count > level + 1).ToList();
                if (descendants.Count > 0)
                {
                    MakeTreeRecursive(level + 1, descendants, child);
                }

            }
        }
    }
    public class Index
    {
        public static List<Index> indexes { get; set; }
        public List<string> paragraphs { get; set; }
        public DataRow row { get; set; }

        public Index() { }
        public Index(DataTable dt)
        {
            Index.indexes =  dt.AsEnumerable()
                .Select(x => new Index() {
                    row = x,
                    paragraphs = x.Field<string>("Index")
                    .Split(new char[] {'.'}, StringSplitOptions.RemoveEmptyEntries)
                    .Select(y=> y).ToList()
                }).ToList();

        }
    }
}

Here is solution if you only want the text

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        DataTable dt = null;
        public Form1()
        {
            InitializeComponent();

            dt = new DataTable();
            dt.Columns.Add("Index", typeof(string));
            dt.Columns.Add("Value", typeof(string));

            dt.Rows.Add(new object[] { "0001.", "aaaa" });
            dt.Rows.Add(new object[] { "0001.0001.", "bbbb" });
            dt.Rows.Add(new object[] { "0001.0002.", "cccc" });
            dt.Rows.Add(new object[] { "0001.0002.0001.", "dddd" });
            dt.Rows.Add(new object[] { "0001.0002.0002.", "eeee" });
            dt.Rows.Add(new object[] { "0001.0003.", "ffff" });
            dt.Rows.Add(new object[] { "0002.", "gggg" });

            new Index(dt);

            MakeTreeRecursive(0, Index.indexes, null);
            treeView1.ExpandAll();

        }
        public void MakeTreeRecursive(int level, List<Index> rows, TreeNode parent)
        {
            TreeNode child = null;

            var groups = rows
                .OrderBy(x => x.paragraphs[level])
                .GroupBy(x => x.paragraphs[level]);

            foreach (var group in groups)
            {
                Index root = group.OrderBy(x => x.paragraphs.Count).First();
                child = new TreeNode(root.row.Field<string>("Value"));

                List<Index> descendants = group.Where(x => x.paragraphs.Count > level + 1).ToList();
                if (descendants.Count()  > 0)
                {
                    MakeTreeRecursive(level + 1, descendants, child);
                }
                if (parent == null)
                {
                    treeView1.Nodes.Add(child);
                }
                else
                {
                    parent.Nodes.Add(child);
                }
            }
        }
    }
    public class Index
    {
        public static List<Index> indexes { get; set; }
        public List<string> paragraphs { get; set; }
        public DataRow row { get; set; }

        public Index() { }
        public Index(DataTable dt)
        {
            Index.indexes = dt.AsEnumerable()
                .Select(x => new Index()
                {
                    row = x,
                    paragraphs = x.Field<string>("Index")
                    .Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(y => y).ToList()
                }).ToList();

        }
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x