-1

I have the following code:

TreeNode parentNode1 = new TreeNode("CONNECTING RODS");
TreeViewNav.Nodes.Add(parentNode1);

string[] subNodes =
{
    "STOCK", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
    "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
};

foreach (var node in subNodes)
{
    parentNode1.ChildNodes.Add(node);
}

so im basically trying to do this but in a neater way:

TreeNode childNodeA = new TreeNode("A");
TreeNode childNodeB = new TreeNode("B");
TreeNode childNodeC = new TreeNode("C");
TreeNode childNodeD = new TreeNode("D");
TreeNode childNodeE = new TreeNode("E");
TreeNode childNodeF = new TreeNode("F");

parentNode1.ChildNodes.Add(childNodeA);
parentNode1.ChildNodes.Add(childNodeB);
parentNode1.ChildNodes.Add(childNodeC);
parentNode1.ChildNodes.Add(childNodeD);
parentNode1.ChildNodes.Add(childNodeE);
parentNode1.ChildNodes.Add(childNodeF);

Im getting the error on the parentNode1.ChildNodes.Add(node); line. The error is

'string' is not assignable to paramenter type 'System.Web.UI.WebControls.TreeNode'

I know its because I have made the array a string array but I dont know how else to do it any help will be really appreciated :)

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Luke Rayner
  • 391
  • 6
  • 20

2 Answers2

1

ChildNodes.Add is expecting a TreeNode object but you are passing it a string. You should:

foreach (var node in subNodes)
{
    parentNode1.ChildNodes.Add(new TreeNode(node));
}

With regards to adding sub-sub nodes:

foreach (var node in subNodes)
{
    var treeNode = new TreeNode(node);
    //Call function that returns all the sub-sub nodes
    //Assign those nodes to 'treeNode' using another foreach - or better still have this as a recursive function
    parentNode1.ChildNodes.Add(treeNode);
}
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • Hmm you were fast, sorry i dint see your answer – Sajeetharan Oct 02 '16 at 16:26
  • @Sajeetharan - It is ok :) I saw that you posted it a moment after so I assumed it is the case :) – Gilad Green Oct 02 '16 at 16:26
  • Thanks sorry to be a pain, I have a function called GetData() which is wehat I use to call the database and inside the parameters it is expecting a sql query but I dont know how to add this to the code that you have shown me – Luke Rayner Oct 02 '16 at 16:44
  • @Luke - where my comments are call your GetData method with the `node`. The result you can assign to the child's of the `treeNode` – Gilad Green Oct 02 '16 at 16:51
  • I still dont understand how this creates a subsub node because to create the sub node I just called the parameter I put in the main node and added a child to that? – Luke Rayner Oct 02 '16 at 17:15
  • @LukeRayner - if you edit your question to include how you call GetData then I can help – Gilad Green Oct 02 '16 at 17:34
1

You shoud use the string of type TreeNode Try this,

foreach (var node in subNodes)
{
    parentNode1.ChildNodes.Add(new TreeNode(node));
}
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • I also want to add ChildNodes to those ChildNodes how would I do this? – Luke Rayner Oct 02 '16 at 16:29
  • You could fine the parent node with this , var result = treeView1.Nodes.OfType() .FirstOrDefault(node => node.Name.Equals("name")); http://stackoverflow.com/questions/24279097/treenode-index-property-by-name – Sajeetharan Oct 02 '16 at 16:32
  • then do the same by calling add(new TreeNode(whatever)); – Sajeetharan Oct 02 '16 at 16:32
  • @LukeRayner - If you show us how you store the values for the sub-sub nodes and how you know to make the connection between sub and sub-sub then we will be able to help better – Gilad Green Oct 02 '16 at 16:33
  • my sub-sub nodes are going to be from a database. I have a column named [Part Number] which is what I want to use to call the data, I want to display the data on the page from a column named [Description] – Luke Rayner Oct 02 '16 at 16:36