|
RadTree1: DataBinding.
|
News
|
Source Code & Description
r.a.d.treeview fully supports binding to datasource (DataSet, DataTable) so you can easily populate your treeview instance with only a few lines of code. The built-in data binding
support auto-binding from a a single self-referencing table with ID -> ParentID relation.
Here're the properties and methods related to data binding:
DataSource - (mandatory) object. Set to an instance of a DataSet or a DataTable
DataMember - (optional) string. If the DataSource is a DataSet and DataMember is set, then r.a.d.treeview assumes the DataTable with the respective name in the DataSet. Otherwise the first DataTable in the DataSet is used.
DataFieldID - (mandatory) string. The name of the DataColumn holding the ID value;
DataFieldParentID - (mandatory) string. The name of the DataColumn holding the ParentID value;
DataBind() - (mandatory) menthod. Call this method after you've set the aforementioned properties per your scenario.
Example:
OleDbConnection dbCon = new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Examples/Functionality/DataBinding/tree.mdb"));
dbCon.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Links", dbCon);
DataSet ds = new DataSet();
adapter.Fill(ds);
RadTree1.DataFieldID = "id";
RadTree1.DataFieldParentID = "parentId";
RadTree1.DataSource = ds;
RadTree1.DataMember = "Links";
RadTree1.DataBind();
r.a.d.treeview automatically binds all fields in each DataRow of the DataTable to its respective RadTreeNode field - e.g.
node.Text is set to the value of the "Text" Column of the row, etc. Same applies for all properties. If you, for some reason, need to customize that or
use alternative mapping, you can hook the NodeBound event and perform custom logic there - r.a.d.treeview calls the
the NodeBound event handler after creating and populating each node. Here's an example - if, for some reason you're storing the Text value in a column that is not
named "Text", you can hook the NodeBound event handler and do custom binding, e.g.
...
private void RadTree1_NodeBound(object o, Telerik.WebControls.RadTreeNodeEventArgs e)
{
DataRow row = e.NodeBoundDataRow;
RadTreeNode node = e.NodeBound;
node.Text = row["CustomText"].ToString();
}
...
We've also provided a short-cur which allows you to do the same with even less code - the Populate method -
simply create a connection, set the DataFieldID & DataFieldParentID properties and call the Populate method with your Sql statement, e.g.
...
OleDbConnection dbCon = new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Examples/Functionality/DataBinding/tree.mdb"));
RadTree1.DataFieldID = "id";
RadTree1.DataFieldParentID = "parentId";
RadTree1.Populate("SELECT * FROM Links", dbCon);
...
Please, view the C# / VB.NET sourece / code tabs for full details.
