0

我在这个结构中有一个表

ListA   labelName
  1      Colorado
  1      Wyoming
  1      Illinois
  2      New York
  2      Ohio

我正在尝试创建一棵树,其中如果 LISTA = 1,它会位于一个名为“header one”的节点下,而科罗拉多州、怀俄明州、伊利诺伊州则作为它的叶子,并且值“2”......通过这样做,我得到了 3“标题一”节点,而不是将所有这三个节点都放在一个...

在此处输入图像描述

SqlCommand cmd = con.CreateCommand();

        comd.CommandText = "SELECT * FROM myTable";
        con.Open();
        SqlDataReader reader = comd.ExecuteReader();
        while (reader.Read())
        {
            City MyData = new City();

            MyData.ListA = reader["ListA"].ToString().Trim();
            MyData.labelName = reader["labelName"].ToString().Trim();
            giveData.Add(MyData);
        }

        int count = 1;

        List<TreeNode> myNode = new List<TreeNode>();
        foreach (City MyData in giveData)
        {
            // 1st foreach
                if (MyData.ListA != "1")
                {

                    TreeNode treeNode = new TreeNode();
                    treeNode.id = count++;
                    treeNode.name = "Header One";
                    treeNode.leaf = false;

                    List<TreeNode> Level1 = new List<TreeNode>();
                    foreach (City labelName  in giveData)
                    {
                        if (labelName.ListA == "1")
                        {// 2nd foreach
                            TreeNode node1 = new TreeNode();
                            node1.id = count++;
                            node1.name = labelName.labelName;
                            node1.leaf = true;

                            Level1.Add(node1);
                        }
                    }

                    treeNode.children = Level1;
                    myNode.Add(treeNode);
            }
            else if (MyData.ListA != "2")
                {

                    TreeNode treeNode = new TreeNode();
                    treeNode.id = count++;
                    treeNode.name = "Header Two";
                    treeNode.leaf = false;

                    List<TreeNode> Level1 = new List<TreeNode>();
                    foreach (City labelName  in giveData)
                    {
                        if (labelName.ListA == "2")
                        {// 2nd foreach
                            TreeNode node1 = new TreeNode();
                            node1.id = count++;
                            node1.name = labelName.labelName;
                            node1.leaf = true;

                            Level1.Add(node1);
                        }
                    }

                    treeNode.children = Level1;
                    myNode.Add(treeNode);
            }
        }
        return JsonConvert.SerializeObject(myNode);

处理此问题的最佳方法是什么

4

1 回答 1

1

您需要做的是将数据分组到ListA.

var groups = giveData.GroupBy(state => state.ListA);

foreach(var group in groups)
{
    //add header to treeview
    string header = group.Key;

    foreach(var state in group)
    {
        //add this state as a child of the group you just added
    }
}

我还建议创建一个查找来帮助您将ListA数字映射到它的文本表示:

var headerLookup = new Dictionary<string, string>()
{
    {"1", "Header One"},
    {"2", "Header Two"},
    {"3", "Header Three"}
};

这将允许您执行以下操作:

string headerText = headerLookup[group.Key];
于 2013-01-16T15:26:44.443 回答