格式化下拉列表的数据源

时间:2011-09-16 15:22:19

标签: c# asp.net linq drop-down-menu

我正在尝试为下拉列表配置数据源。 数据库表包含字段“(id, value, parenttypeid) 父类型可以是0或1,2..etc, 我想根据父母和孩子填充下拉列表 可以显示为 - :>

Parent1
    Child1
    Child2
Parent 2

我通过排序结果尝试了这个但是无法获得格式。 如果有人有想法请回复 感谢

3 个答案:

答案 0 :(得分:1)

我认为您可以先对ValueparentTypeId进行排序来解决问题。

虽然如果您发布了一些代码会有所帮助,但如果您需要帮助。

我想我觉得它看起来像这样:

var myList = myThings
   .OrderBy(t => t.Value)
   .ThenBy(t.ParentTypeId)
   .Select(t => new {
       Text = t.Value,
       Value = t.Id 
   });

答案 1 :(得分:1)

    class Node
    {
        public string Name { get; set; }
        public List<Node> Child { get; private set; }

        public Node(string name)
        {
            Child = new List<Node>();
            Name = name;
        }
    }

    static void Main(string[] args)
    {
        var listItems = new List<string>();
        var root = new Node("Root");
        root.Child.Add(new Node("1"));
        root.Child.Add(new Node("3"));
        var node = new Node("4");
        var nd = new Node("5");
        nd.Child.Add(new Node("7"));
        node.Child.Add(nd);
        node.Child.Add(new Node("6"));
        root.Child.Add(node);

        GetNames(root, listItems);

        foreach (var listItem in listItems)
        {
            Console.WriteLine(listItem);
        }
    }

    static void GetNames(Node parent, List<string> result)
    {
        result.Add(parent.Name);
        foreach (var child in parent.Child)
        {
            GetNames(child, result);
        }
    }

screen

答案 2 :(得分:1)

如何在前面添加string到子元素

if(parent)
   return element;
else
  return " - "+ element;
相关问题