2011-01-13 7 views
0

J'ai un TreeView sur ma principale formeComment ajouter un nœud enfant à un nœud enfant dynamiquement ajouté

J'ai mon code d'un de la forme principale est comme suit

Buttonclick 

StrNode = string.Empty; 
StrNode = "Batch" + Append.Batchcnt.ToString() + "(" + strSelectedClassCode + ")"; 
frmmain.loadFromForm(StrNode, true, strSelectedClassCode); 

Sur mon principale forme que j'ai mon code comme suit

public void loadFromForm(string strNode, bool bResult, string strStandardClsCode) 
    { 
     if (Append.oldbatchcontrol != strNode) 
     { 
      if (tvwACH.SelectedNode.Text == "FileHeader") 
      { 
       tvwACH.SelectedNode.Nodes.Add(strNode); 
      } 
      if (tvwACH.SelectedNode.Text == "BatchHeader") 
      { 
       tvwACH.SelectedNode.Nodes.Add(strNode);// After this i have to add another node as a child to that added node and also if a node with particular name exists i would like to write the text with a count value appended 
      } 
    } 
     } 

Alors que mon TreeView devrait être comme suit

ACH 
|->Some.txt 
    |->Fileheader 
    |->BatchHeader 
     |->Batch1 
      |->Entry1 
      |->Entry2 and so on // These two should be added dynamically after that Batch1 

Répondre

2

Utilisez ce lieu:

public void loadFromForm(string strNode, bool bResult, string strStandardClsCode) 
    { 
     if (Append.oldbatchcontrol != strNode) 
     { 
      if (tvwACH.SelectedNode.Text == "FileHeader") 
      { 
       tvwACH.SelectedNode.Nodes.Add(strNode); 
      } 
      if (tvwACH.SelectedNode.Text == "BatchHeader") 
      { 
       TreeNode node = tvwACH.SelectedNode.Nodes.Add(strNode,strNode);// After this i have to add another node as a child to that added node and also if a node with particular name exists i would like to write the text with a count value appended 
       node.Nodes.Add(...); 
      } 
    } 
} 
+0

Ok, mais comment puis-je vérifier et ajouter le cnt si le nœud existe – Dotnet

+0

Merci pour l'idée que j'ai eu selon mon exigence – Dotnet

1

Vous devez généralement une fonction récursive pour construire un arbre. Par exemple:

private void AddNode(NodeParent, Data) 
{ 
    Node oNode; 

    //Check if this is the first node 
    if (NodeParent ==null) 
    { 
     oNode = new Node(Data.Name); 
    } 

    //Step though each child item in the data 
    foreach(DataItem in Data) 
    { 
     //Add the node 
     this.AddNode(oNode, DataItem); 
    } 

    oNode.Nodes.Add(new Node(Data)); 
} 

Ce code est un petit guide, mais il devrait vous donner une idée.

Questions connexes