0

I have a table in this structure

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

I am trying to create a tree where if LISTA = 1, it goes under one node called "header one" and Colorado, Wyoming, Illinois as its Leaf and samething with value "2"... by doing this I am getting 3 "Header one" nodes instead of having all those three nodes under one...

enter image description here

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);

What would be the best way to handle this

EagleFox
  • 1,367
  • 10
  • 34
  • 58

1 Answers1

1

What you need to do is group the data on 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
    }
}

I'd also suggest creating a lookup to help you map the ListA number to it's textual representation:

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

This will allow you to do the following:

string headerText = headerLookup[group.Key];
Servy
  • 202,030
  • 26
  • 332
  • 449
  • Thanks Servy... I still can't get the grouping right... also the headerText does not appear to like the KEY part...would you by any change have a working sample of this kind... – EagleFox Jan 16 '13 at 15:59
  • @EagleFox Saying "it doesn't work" means nothing. If you're getting an error message, state what it is. You have not provided enough of your code for me to write code that I can compile, but this will be at the very least almost exactly what you want, you will only need to make minor modifications based on the information you haven't provided. – Servy Jan 16 '13 at 16:06
  • Thanks Servy... I didn't mean to say your answer doesn't work... What I meant was The groupBy method that you mentioned still gives the same 3 nodes... there is no error... Although I was able to accomplish this by using dictionary... and for the textual representation I just used case statements... Thanks for your help though... you mentioned dictionary on your answer and I thought why not use it for my entire list :) – EagleFox Jan 16 '13 at 16:18
  • 1
    @EagleFox If your headers are being added multiple times then you did something different, or didn't follow this code. If you only add top level nodes in the outer `foreach`, you won't end up with more than one per header. – Servy Jan 16 '13 at 16:49
  • Aaaah I C... Bingo... I was missing the second foreach... for somereason I don't know why I only saw one... yep... this way works fine too... Thanks a bunch for your time Servy – EagleFox Jan 16 '13 at 17:02