VB.NET - add edit delete nodes and sub nodes in tree view

Asked By anjali sri on 14-Mar-12 12:50 AM
Earn up to 10 extra points for answering this tough question.
how can add , edit, delete nodes and sub nodes  in treeview in vb.net 2005 and sql server2005
Donald Ross replied to anjali sri on 14-Mar-12 11:28 PM
Anju,

Check out the forum post by Reena from back in Dec 2010
http://www.eggheadcafe.com/community/csharp/2/10236959/how-to-add-nodessubnodes-programmatically-to-treeview.aspx

This may help you.  additionally you can search the forum for others who have posted similar questions and hopefully find where it may have already been answered.

HTH  Don

Somesh Yadav replied to anjali sri on 15-Mar-12 01:31 AM

Class Hierarchy of TreeView Control

TreeView
TreeNodeCollection
TreeNode

TreeView is contains collection of TreeNodes. Each TreeNode is a member of TreeNodeCollection.
TreeNode is a type of recursive Data Structure as itself contains Collection of TreeNodes. All TreeNodes are number from 0 to Nodes.Count-1 sequentially in the order of appearance irrespective of the hierarchial structure

eg.
ROOT
T1
T11
T2
For the above structure the nodes are numbered as
ROOT - 1
T1 - 2
T11 - 3
T2 - 4

1.Creating TreeView

TreeView is created by instancing the TreeView class.

eg.
TreeView tvSample = new TreeView();

2.Add Nodes to TreeView

The Nodes property of the TreeView is an instance of TreeNodeCollection class
which is used to Add/Remove Nodes.

eg.
tvSample.Nodes.Add("Sample")
or
tvSample.Nodes.Add(
new TreeNode("Sample"))

3.Removing Nodes from TreeView

The Nodes Property of the TreeView is an instance of TreeNodeCollection class
which is used to Add/Remove Nodes.

eg.
tvSample.Nodes.RemoveAt(1) //To Remove the First appearing node in the Tree.
or
tvSample.Nodes.Remove(tvSample.SelectedNode) //Removes the currently selected node.

4.Determine the SelectedNode

SelectedNode property is used to determine the currently selected node in the
Tree.

eg.
string nodeText=tvSample.SelectedNode.Text;
will retrieve the Text of the selected node.


for more information refer to the below link,

http://www.c-sharpcorner.com/UploadFile/prasadh/TreeViewControlInWinFormsPSD11182005235448PM/TreeViewControlInWinFormsPSD.aspx

hope it helps you.

Reena Jain replied to anjali sri on 15-Mar-12 04:42 AM
Hi,

You can bind the treeview with database using storedprocedure. For example stored procedure name is "ProcSelect". So try this code

private TreeView tView = new TreeView();
public void PopulateTree()
{
  DataTable dt = new DataTable();
  Hashtable ht = new Hashtable();
 
  SqlConnection conn = new SqlConnection("data source=servernm;initial catalog=dbname;integrated security=SSPI;");
 
  new SqlDataAdapter("ProcSelect", conn).Fill(dt);
 
  foreach (DataRow r in dt.Rows)
  {
  int nodeLvl = int.Parse(r["NodeLevel"].ToString());
  string nodeParent = r["Parent"].ToString();
  string nodeName = r["Name"].ToString();
 
  TreeNode tNode = new TreeNode(nodeName);
  ht.Add(nodeLvl.ToString() + nodeName, tNode);
 
  if (tView.Nodes.Count == 0)
  tView.Nodes.Add(tNode);
  else
  {
  nodeLvl --;
  ((TreeNode) ht[nodeLvl.ToString() + nodeParent]).Nodes.Add(tNode);
  }
  }
  ht.Clear();
}
anjali sri replied to Reena Jain on 15-Mar-12 04:59 AM
can you please tell me without stored procedure i'm not using stored procedure
Reena Jain replied to Reena Jain on 17-Mar-12 05:00 AM
Hi,

sure you can use select query in place of stored procedure like this

new SqlDataAdapter("select * from table1", conn).Fill(dt);

this will fill datatable with the table1 data of database