I have an access database from which i have to retrieve data from access database into c# showing tree view in my application. here is what i want to do. I have this access database with the following tables.

categoryid   | Category Name | Parent Category 
===============================================
1000            Food              996
996            contemporary       13
995            Anything           13
13             Glassware          50693

This is just an example so now when the tree is displayed it should be like this

Glassware (13)
||
  -> Contemporary (996)
      ||
         -> Food  (1000)

  -> Anything   (995)

Like this there are numerous parent and child categories. I am trying to find an algorithm in c Sharp which will do this for me.

I appreciate any help which i can get

Recommended Answers

All 4 Replies

Hey there,
What could you do is to run a SELECT statement like this:

SELECT * FROM [TableName]

After you got the query results into a DataTable Object, you should run a For each loop like this:

For each(DataRow dr in DataTable)
{
  TreeNode trnode = new TreeNode();

  if(dr["ParentCategory"].Value == 0)
  {
   trnode.text = dr["CategoryName"].Value.ToString();
   trnode.Tag = dr["CategoryId"].value.toString();
   TreeView.nodes.Add(trnode);
  }
  else
  {
   TreeNode parent = //Find the ParentNode from the Tag Property
   trnode.text = dr["CategoryName"].Value.ToString();
   trnode.Tag = dr["CategoryId"].value.toString();
   parent.Add(trnode);
  }
}

Note that in order for this to work you have to spesify that the nodes that don't have a parent( which means that they are parent nodes) have to have a unique number (like 0 or whatever you like).

Cheers.

Thanks for your help but that didn't quite work for what i was looking for. This algorithm will work only when the DB table is sorted, but my table is not. It is not necessary that a child's parent row arrives prior to its row but a parent row can arrive after in the table.

Then modify your sql statement to look like this, so the parents should come first and then the children :) :

SELECT * FROM [TableName] ORDER BY ASC


Hope i helped this time :p

HI Friends,

i am also looking for a code which will show the employees of a organization as tree. it will take input from a database server. it will be more more helpful if you can show it as a web page.

if we click or point the mouse over an employee it will show all the details of that employee.

can you please tell me that is it possible in php and java script or not?

thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.