There may be the situations when it would be required to bind
the XTreeView programmatically. Sometimes it becomes necessary to have more
control on what an extended control does implicitly. XTreeView has property
DataSet that is to be set programmatically after populating it with tables and
setting its relations. The following code listing shows this.
Listing 6
SqlConnection dbCon = new
SqlConnection("Data Source=gmohyd-serv;Initial
Catalog=pakistan_db;Integrated Security=True");
dbCon.Open();
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Provinces", dbCon);
DataTable parentTable = new DataTable("Provinces ");
adapter.Fill(parentTable);
ds.Tables.Add(parentTable);
adapter = new SqlDataAdapter("SELECT * FROM Divisions", dbCon);
DataTable childTable = new DataTable("Divisions");
adapter.Fill(childTable);
ds.Tables.Add(childTable);
adapter = new SqlDataAdapter("SELECT * FROM Districts", dbCon);
DataTable gchildTable = new DataTable("Districts ");
adapter.Fill(gchildTable);
ds.Tables.Add(gchildTable);
….
Binding XTreeView is no longer a difficult job to do now.
First, you have to take a connection object and open it. Then take a DataSet
object and initialize it. Take SQLDataAdapter object and fill the parent table
through it. Do not forget to name the table while initializing the DataTable
object because it will be used implicitly by the table name property of
XTreeView. Then add it to the table collection of the dataset. Repeat the same
steps for each next child table. In our case we have three tables, Provinces,
Division and Districts. Though populating the dataset with the tables has been
accomplished, we are still left with setting the fields’ mandatory relationship
properties of the control.
Listing 7
XTreeView1.ParentChildRealtionField = "ProvinceID";
XTreeView1.ChildParentRelationField = "ProvinceID";
XTreeView1.ChildGrandChildRelationField = "DivisionID";
XTreeView1.GrandChildRelationField = "DivisionID";
XTreeView1.DataSet = ds;
These properties are self explanatory and also have been
explained earlier. The important point here is that even programmatically, much
of the effort of binding and establishing relationship between tables has been
lifted through these properties. You can still use the table text field and
table value filed properties which are optional, otherwise default will be
used. You can set the table value field property with the field name that
contains the corresponding pages path if any (only from root directory or sub
folders). In this way, you can take advantage of navigation using database or
side-by-side with sitemap. Lastly, you have to set the DataSet property of
XTreeView with the populated dataset.