Click here to Skip to main content
15,906,463 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Table structure is like below with groupID,GroupName,ParentGrID.

It is like Level Structure.

kindly help me through C# coding or SQL Stored Procedure to get the Root Group and all child Group.

B->B1->B11..........................



GroupID 	GroupName 	ParentGroupID
1 	A 	0
2 	A1 	1
3 	B 	0
4 	B1 	3
5 	B2 	3
6 	B11 	4
7 	B3 	3
8 	C 	0
9 	D 	0
Posted
Updated 24-Nov-11 22:12pm
v2
Comments
[no name] 25-Nov-11 4:13am    
please make your question more descriptive so that we could help you with your problem. Thanks.
manasBonton 25-Nov-11 4:24am    
i have some group list those have parentGRID =0,these are root nodes. after that some child nodes are there of corresponding groups. i want to get the root node (B) and their child node(B1,B2,B3....)as well as subchild nodes(B11,B12......)
Amir Mahfoozi 25-Nov-11 4:48am    
http://www.codeproject.com/KB/database/TreeStructureWithDatabase.aspx

1 solution

you can do it with below code in c#
// not tested in detail , but surly help
C#
string data = "";
            DataTable dt = new DataTable();
            DataRow[] DR_DT;
            foreach (DataRow dr in dt.Rows)
            {
                DR_DT = dt.Select("ParnetGroupId = '" + dr["GroupId"] + "'");
                for (int i = 0; i < DR_DT.Length; i++)
                {
                    data += DR_DT[i]["GroupName"].ToString();
                    DR_DT = dt.Select("ParnetGroupId = '" + DR_DT[i]["GroupId"] + "'");
                    while(DR_DT.Length> 0)
                    {
                        data += DR_DT[i]["GroupName"].ToString();
                        DR_DT = dt.Select("ParnetGroupId = '" + DR_DT[i]["GroupId"] + "'");
                    }
                }
                
            }


mark as solution if its solved your problem
 
Share this answer
 
Comments
RaviRanjanKr 25-Nov-11 15:56pm    
5+

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