Generally, binding a TreeView with database requires much to
do with code. It requires opening connection, populating a dataset with all
required tables and establishing the relationship between them. XTreeView
minimizes this effort by providing you few properties to bind it with database
directly. Before mentioning these properties, I would like to explain shortly
the necessary information ASP.NET TreeView requires for binding with database.
Generally binding a TreeView with database requires the
following steps.
Open Connection
Populate Dataset with the parent table.
Populate Dataset with the child table if any.
Establish the relation between these two tables added to the
dataset on the basis of the primary and foreign key relationship. (This can be
achieved by taking a Relation object, instantiating it with parent child field
information and adding it to the Relation collection of the Dataset.)
Repeat the previous two steps for each next child table if
any. (The relation will be maintained between the current child and its
immediate parent table.)
Now create the nodes of the tree recursively for each row of
the Dataset’s parent table with all related rows of the Dataset’s child table
under that primary key.
The following figure shows all the properties for XTreeView
binding with database.
Figure 4
Now let us take a look at these properties that do the same
job for us without writing any code. I have put these properties under a new
category database in the property pane for the sake of simplicity and better
understanding.
ConnectionString property is mandatory and required to be
set with a valid connection string as shown below.
Listing 4
<cc1:XTreeView ID="XTreeView1" runat="server"
ConnectionString="Data Source=gmohyd-serv;Initial Catalog=pakistan_db;Integrated Security=True"
......
</cc1:XtreeView>
Parent node has the following properties.
ParentTableName: Name of the parent table. (Mandatory)
ParentNodeTextField: Name of the field of parent table to be
displayed as the node text. (Optional, if not set XtreeView takes the second
column as node text field.)
ParentNodeValueField: Name of the field of parent table that
has to be set as value. Value field can be used for saving page URL's if
required, otherwise it is suitable to save ID's. (Optional, if not set,
XtreeView takes the first column as node text field.)
ParentChildRelationField: Name of the field (primary key)
of parent table that is being used as foreign key in child table. (Mandatory if
there is some child of it.)
The child has the following properties.
ChildTableName: Name of the Child table. (Mandatory)
ChildNodeTextField: Name of the field of Child table to be
displayed as the node text. (Optional, if not set, XtreeView takes the second
column as node text field.)
ChildNodeValueField: Name of the field of Child table that
has to be set as value. Value field can be used for saving page URL's if
required, otherwise it is suitable to save ID's. (Optional, if not set,
XtreeView takes the first column as node text field.)
ChildParentRelationField: Name of the field (foreign key)
of the child table that is being used as primary key in parent table.
(Mandatory)
ChildGrandChildRelationField: Name of the field (primary
key) of this immediate parent table that is being used as foreign key in grand
child table. (Mandatory, if there is, it is child)
Grand Child has the following properties.
GrandChildTableName: Name of the Grand Child table. (Mandatory)
GrandChildNodeTextField: Name of the field of Grand Child
table to be displayed as the node text. (Optional, if not set, XtreeView takes
the second column as node text field.)
GrandChildNodeValueField: Name of the field of Grand Child
table that has to be set as the value. Value field can be used for saving page
URL's if required, otherwise it is suitable to save ID's. (Optional, if not set,
XtreeView takes the first column as node text field.)
GrandChildChildRelationField: Name of the field (foreign
key) in the grand child table that is being used as primary key in immediate
parent table. (Mandatory)
You may have noticed that most of the properties are optiona;l
if you do not set them, they are set with default fields. For binding
XTreeView with relational database we need the following information:
Name of table(s)
Name of the relation field(s) with (immediate) parent and
(immediate) child, a primary key field in case of parent table and foreign key
field in case of child table.