954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Algorithm to get data from access database to tree view in C #

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

csolutions
Newbie Poster
2 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

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.

Alexpap
Junior Poster
121 posts since Sep 2008
Reputation Points: 11
Solved Threads: 13
 

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.

csolutions
Newbie Poster
2 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

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

Alexpap
Junior Poster
121 posts since Sep 2008
Reputation Points: 11
Solved Threads: 13
 

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.

sayantanbagchi
Newbie Poster
1 post since May 2012
Reputation Points: 0
Solved Threads: 0
 

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: