Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I AM CREATING AN EXAMPLE in c# using SQL WHERE I HAVE TO SHOW THE COMPANY NAME WITH "PARENTID" , "0" IN COMBO BOX AND ITS PRODUCTS AND SUB PRODUCTS IN THE TREE VIEW CONTROL Based on THE COMBO BOX SELECTION.
THE FORMAT OF MY TABLE IN SQLTABLE IS

ID          NAME                PARENTID
1           DELL                  0
2           IBM                   0
1.1         COMPUTER              1
1.2         PRINTER               1
1.1.1       RAM                   1.1

SO IF I SELECT "DELL" in the combobox

I SHOULD BE ABLE TO SEE

"THE COMPUTER AND PRINTER"

AND ON FURTHER EXPANSION OF tree control

"RAM"

and ALL THIS IS STORED IN ARRAY COLLECTION VARIABLE. NEED HELP?
Posted
Comments
BillWoodruff 7-Feb-12 22:31pm    
I would certainly start by examining the possibility of binding the TreeView contents to your data: and then consider ... assuming the SQL Tables need transformation into another form ... to be usable for data-binding to the TreeView ... what's required for that transformation.

Start by searching here on CP and StackOverFlow on "treeview" and "data binding."

1 solution

it will be some thing like that
C#
var fathers = GetParents().ToList();

getParents to get all the records that have 0 in it's parent Id. then

C#
foreach (var f in fathers)
{
    _cachedFathers.Add(f);
    MyChilds(f);
}


C#
private void MyChilds(Company comp)
{
    tvi.Children = new ObservableCollection<JobDto>();
    var childs = GetParents(tvi.CompanyId).ToList();
    foreach (var c in childs)
    {
        comp.Children.Add(c);
    }
}

GetParents(int companyId) shold give you all the companies that have "companyId" in the ParentId column

and in the Company Calsss you should have
XML
public Company Parent { get; set; }

private ObservableCollection<Company> _children = new ObservableCollection<Company>();
public ObservableCollection<Company> Children
{
    get { return _children; }
    set { _children = value; OnPropertyChanged("Children"); }
}


that will give you an object fathers full of hierarchy data

Hope this help
 
Share this answer
 

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