Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want TreeView like:-

A-1 (Parent Node)
---A01-(Sub Node of A)
-------A0101-(Sub Node of A01)
........
B-1 (Parent Node)
----B01-(Sub Node of B)
-------B0101-(Sub Node of B01)
.......
C-1 (Parent Node)
.
.
.

What I have tried:

C#
SqlCommand cmd = new SqlCommand("SELECT SUBSTRING(BCD,1,1)[PARENT], BCD, DESCRIPTION from BALANCE_SHEET where TYPE = 'S' GROUP BY BCD, DESCRIPTION", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(DT);

var treeParent = DT.AsEnumerable().Select(a => a.Field<string>("parent")).Distinct();
int count = 0;

foreach(string row in treeparent)
{
    var des = DT.AsEnumerable().Where(b => b.Field <string>("BCD") == row).CopyToDataTable();
    
    string ParentNode = row + " - " + Des.Rows[0]["DESCRIPTION"].ToString();
    TreeViewBcd.Nodes.Add(ParentNode);

    var dtnodeList = DT.AsEnumerable().Where(c => c.Field<string>("BCD").Contains(row) && !Equals(row.Substring(0))).CopyToDataTable();
    
    foreach(DataRow dataRow in dtnodeList.Rows)
    {
        if (!dataRow["BCD"].ToString().Equals(row))
        {
            string childNode = dataRow.ItemArray[1].ToString() + " - " + dataRow.ItemArray[2].ToString();
            TreeViewBcd.Nodes[count].Nodes.Add(childNode);
        }
    }
    
    count++;
}

con.Close();
Posted
Updated 5-Nov-19 2:47am
v5
Comments
Richard Deeming 24-Oct-19 12:17pm    
The editor managed to break your code formatting. I think I've managed to fix it now.

Now you need to explain what the problem is.
Kornfeld Eliyahu Peter 27-Oct-19 10:43am    
Do I understands correctly (from your SQL) that the first letter of a node's ID is the parent node's ID? Now that's quite not fit the sample you gave above...
To create a nice tree binding, you should come back from the SQL with 3 columns for each node: parent id, node id, text (where parent id will be null for the root(2))...
j snooze 4-Nov-19 17:33pm    
I'm guessing you want your SQL results something closer what this little SQL sample would provide...this is assuming your BCD value is consistently 1 character and child values are continually 2 characters long appended to the root character. So this just returns the parent as the BCD less 2 characters... Since I don't know if this is the case or not, it is something to spark an idea to maybe help you finish whatever you are trying to accomplish.

select case
when len(BCD) = 1 Then BCD
Else substring(BCD,1,len(BCD)-2)
End as Parent,
BCD,
Description
from Balance_Sheet where type = 'S'

1 solution

I'm guessing you want your SQL results something closer what this little SQL sample would provide...this is assuming your BCD value is consistently 1 character and child values are continually 2 characters long appended to the root character. So this just returns the parent as the BCD less 2 characters... Since I don't know if this is the case or not, it is something to spark an idea to maybe help you finish whatever you are trying to accomplish.

select case
when len(BCD) = 1 Then BCD
Else substring(BCD,1,len(BCD)-2)
End as Parent,
BCD,
Description
from Balance_Sheet where type = 'S'
 
Share this answer
 
Comments
Member 14633609 5-Nov-19 8:44am    
I want TreeView like A,L and P is Parent node and
A01,A02,A03 child of A and A0101,A0102,A0103 child of A01 and A010101,A010102,A010103 child of A0101---

L01,L02,L03 child of L and L0101,L0102,L0103 child of L01 and L010101,L010102,L010103 child of L0101--

P01,P02,P03 child of P and P0101,P0102,P0103 child of P01 and P010101,P010102,P010103 child of P0101

upto 10th level
Richard Deeming 5-Nov-19 10:54am    
Are the child IDs always exactly two characters longer than the parent IDs?
Member 14633609 6-Nov-19 5:42am    
Yes...Like A01 has A0101,A0102,A0103 child.
Member 14633609 6-Nov-19 5:43am    
Please, Provide me Complete C# code

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