Click here to Skip to main content
15,888,031 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Form APplication contains Button,TextBox,Treeview and a Table that contains
columns MenuID,FormName,Assembly,Caption,ParentID,OrderNo,Deleted.
Here are some sql queries,
select * from  tblMenu where menuid=87
select * from  tblMenu where menuid=23
select * from  tblMenu where menuid=1
select * from  tblMenu where menuid=0


Results:
MenuID       ParentID 
 
87              23
23              1
1               0    
0          	0


Look at query result, MenuID 87 have ParentID 23, then MenuID 23 have parentID1, then MenuID 1 have parentID 0.
when i write any MenuID in textbox then it shows the all the ParentID, like in above question when i write 87 in the
textbox it make the treeview of all ParentID 0,1,23.
Kindly tell me through coding.thanks
Hope now you can better understand my question.
kindly help me that how that is possible through coding,


Please help me, thanks.
Posted
Updated 19-Oct-11 0:27am
v9
Comments
Xeshan Ahmed 18-Oct-11 8:09am    
you want to make tree-view in what order plx do elaborate this question
bilawal121 19-Oct-11 6:22am    
My Form APplication contains Button,TextBox,Treeview and a Table that contains
columns MenuID,FormName,Assembly,Caption,ParentID,OrderNo,Deleted.
Here are some sql queries,
<pre>
select * from tblMenu where menuid=87
select * from tblMenu where menuid=23
select * from tblMenu where menuid=1
select * from tblMenu where menuid=0
</pre>

Results:
<pre>
MenuID ParentID

87 23
23 1
1 0
0 0
</pre>

Look at query result, MenuID 87 have ParentID 23, then MenuID 23 have parentID1, then MenuID 1 have parentID 0.
when i write any MenuID in textbox then it shows the all the ParentID, like in above question when i write 87 in the
textbox it make the treeview of all ParentID 0,1,23.

Hope now you can better understand my question.
kindly help me that how that is possible through coding,


Please help me, thanks.
BillWoodruff 18-Oct-11 19:53pm    
I don't understand what your question is. You are, evidently, adding items to a TreeView based on geting a number in a TextBox and looking up the corresponding record in a Table. So what's going wrong ? What's the error ?
bilawal121 19-Oct-11 6:23am    
My Form APplication contains Button,TextBox,Treeview and a Table that contains
columns MenuID,FormName,Assembly,Caption,ParentID,OrderNo,Deleted.
Here are some sql queries,
<pre>
select * from tblMenu where menuid=87
select * from tblMenu where menuid=23
select * from tblMenu where menuid=1
select * from tblMenu where menuid=0
</pre>

Results:
<pre>
MenuID ParentID

87 23
23 1
1 0
0 0
</pre>

Look at query result, MenuID 87 have ParentID 23, then MenuID 23 have parentID1, then MenuID 1 have parentID 0.
when i write any MenuID in textbox then it shows the all the ParentID, like in above question when i write 87 in the
textbox it make the treeview of all ParentID 0,1,23.

Hope now you can better understand my question.
kindly help me that how that is possible through coding,


Please help me, thanks.
BillWoodruff 19-Oct-11 7:25am    
So you want to create a TreeView like this:

23
87
1
23
0
1
0

?

See if this helps you get started:
XML
// put a TreeView named 'treeView1 on a WinForm
// put a Button named 'button 1 on the Form
// set 'button1's Click Event handler
// to button1_Click in the design-time Properties/Events facility
// run the project, click the button: observe
//
private TreeNodeCollection TNCollection;

private TreeNode node1;

private void BuildTree(List<int> intList )
{
    TNCollection = treeView1.Nodes;

    // edit #1 : sort the list of integers
    intList.Sort();

    foreach (int theInt in intList)
    {
        node1 = new TreeNode(theInt.ToString());
        TNCollection.Add(node1);
        TNCollection = node1.Nodes;
    }

    treeView1.ExpandAll();
}

private void button1_Click(object sender, EventArgs e)
{
    // edit #1: list of integers now scrambled to demonstrate
    // sorting the list works, and that duplicates are handled
    BuildTree(new List<int> {23, 1, 0, 44, 87, 0, 23});
}
 
Share this answer
 
v4
Comments
bilawal121 21-Oct-11 6:27am    
Thanks alot sir
First, if the structure is conventional, order the queries by ID so each node's parent is already available when that node is created. Then, pseudocode:
Map<int,Node> nodes;
nodes[0] = new Node("Root");
foreach(TableRow row in table){
 Node node = new Node(row.Text); // you have a text column right?
 Node parent = nodes[row.ParentID];
 parent.Children.Add(node);
 nodes[row.MenuID] = node;
}
 
Share this answer
 
You need 2 methods, the first method creates the nodes for the records that have 0 as the parent. I put the record into the tag property of the node (TreeViewItem I believe)

The second method receives the parent node and looks for and records with the parentID matching the record in the tag property of the parent node passed in.

Having added the child node the second method then calls itself (recursive processing) passing in the newly created node as the parent.

This allows you to build a treeview of any depth. Also the supporting object now resides in the tag property and can be used when you need to interact with the treeview.
 
Share this answer
 
Comments
bilawal121 19-Oct-11 6:23am    
kindly tell me the solution through coding
Mycroft Holmes 19-Oct-11 6:51am    
Sorry I don't have the time nor the inclination to do your work for you
bilawal121 20-Oct-11 7:08am    
Sir i have completed my task aproximately half,
i get all the ParentId against the MenuId i have enter,
like i enter MenuID=87 it gives me all the ParentId's like 0,1,23 .
sir kindly tell me the treeview of that ParentID's
like this
0
1
23
0 should be parent of 1,and 1 should be parent of 23.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900