Binding Hierarchical Data from SQL Server In TreeView Control

Hi Expert, we have two tables in sql server database as below,

1- Groups Table
2-Vehicle Table

Groups table having following columns

Vehicle Table having following columns

Now we need to bind hierarchical data in Treeview webform control , it should be display
group wise data.

Please provide code in  with

Thanks in advance.
Asrar AzmiWeb DeveloperAsked:
Who is Participating?
Asrar AzmiWeb DeveloperAuthor Commented:
Dear Expert,

I Know these two article i have seen it before , but this is not fulfill my requirement.
Rose BabuSenior Team ManagerCommented:
can you post some of sample data for those two tables?
Guy Hengel [angelIII / a3]Billing EngineerCommented:
here is the relevant #C code I have set up, but still testing some things ...

dr is a SqlDataReader in my case
treeData is the treeview control

//dataset to hold the data, set up relations etc ...
DataSet ds = new DataSet();

// load the data from a normal datareader (see below for the query)
// note the "x" here is used later in the xml XPath !
ds.Load(dr, LoadOption.OverwriteChanges, "x");

// we will refer to 1 single data table, the first one
DataTable dt = ds.Tables[0];

// we want to set up the hierarchical relation (see in query
DataRelation relation = new DataRelation("parentchild", dt.Columns["id"], dt.Columns["pid"], true);

// to make sure we could have several levels of hierarchy
relation.Nested = true;

// add and apply that relation to the data set

// this small detail is required to get the XML formatted as needed for the treeview bindings
foreach (DataColumn column in dt.Columns)
  column.ColumnMapping = MappingType.Attribute;

// now, extract the XML from the data source
XmlDataSource xml = new XmlDataSource();

// for, we need to assign ID values to the XML, otherwise you get some errors
xml.ID = "XML_" + pk;
treeData.ID = xml.ID;

xml.Data = ds.GetXml();
// specify what the Xpath should be, the "x" coming from the dataset.Load function call above
xml.XPath = "/NewDataSet/x";

// set up a binding rule
                        TreeNodeBinding b = new TreeNodeBinding();
                        b.TextField = "data";
                        b.ValueField = "id";
                         /* the rest can be added, fields to be added to the query
                        b.ImageUrlField = "icon";
                        b.ImageToolTipField = "tooltip";
                        b.NavigateUrlField = "url";
                        b.ToolTipField = "tooltip";
                        b.TargetField = "target";
                        b.PopulateOnDemand = false;

                /// add and apply the binding and xml datasource to the treeview
                        treeData.DataSource = xml;

Open in new window

the query, for your table for above needs to return (at least) the 3 following columns:

this select suggestion assumes that UpID  is groups points to the parent "group", and UpID  in vehicles points to the "group" the vehicle belongs to.
select 'G' + cast(g.ID as varchar(100)) id 
, g.GroupName data 
, 'G' + cast(g.UpID as varchar(100)) pid
  from groups
select 'V' + cast( as varchar(100)) id
 , v.VehicleName data
 , 'G' + cast(v.UpID as varchar(100)) pid
 from Vehicle v

Open in new window

hope this helps, sorry for the long delay
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.