Question

I am consuming the betfair API in a c# windows form application,

when i call the getAllMarket() method, i get a long string which includes a no of sting paths like this:

~\Cricket\Group C\England v South Africa\Test Series~  
~\Cricket\Group C\English Domestic\Clydesdale Bank 40 2012\Group Winners~ 
~\Cricket\Group C\England v South Africa\Test Series\England v South Africa (2nd Test)~

so i want to populate a treeview control according to this paths like this:

step 1>Check if the path already exixts, then select the last node on the path, 
step 2>else create the whole path and select the last node,
step 3> add a custom child node to he selected last node of the path,

i have never worked with treeviews earlier, but have now learnt the basics and the custom child node is also created already, so a detailed answer would be really helpfull, thanxx in advance

Was it helpful?

Solution

it was for wpf so in windows forms the code should be

        List<string> mylist = market.Trim('~').Split(new string[] { @"\" }, StringSplitOptions.RemoveEmptyEntries).ToList();
        if (mylist.Count > 0)
        {
            TreeNode root = new TreeNode(mylist[0]);
            treeView1.Nodes.Add(root);
            mylist.RemoveAt(0);

            TreeNode temp = root;
            foreach (string s in mylist)
            {
                temp = AddNode(temp, s);
            }

            treeView1.SelectedNode = root;
        }

the first loop means if the node exists return it, else create it & return it

           private TreeNode AddNode(TreeNode parent, string header)
    {
        foreach (TreeNode subitem in parent.Nodes)
            if (subitem.Text.ToString() == header)
                return subitem;
        TreeNode tvi = new TreeNode(header);
        parent.Nodes.Add(tvi);
        return tvi;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top