I have a DataSet that already has a hierarchical relationship between the two DataTables it contains and I want to bind this to a two-level WebDataTree. I can't find any examples of how to do this. Just setting the DataSource to the DataSet doesn't seem to work. Our architecture involves a DAL layer and a business layer that returns either custom object collections, or sometimes DataTables/DataSets.
Can someone help me with this? I want to use this new control but this has me stumped.
Ideally for each of the two levels I can set the visible text of the node to a description field from the corresponding DataTable and the value to an id field so that I can refresh a WebDataGrid based on the clicked node.
Thanks.
Hi,
Here is a simple example of WebDataTree bound to a DataSet with load-on-demand enabled:
<ig:WebDataTree ID="WebDataTree1" runat="server" Height="500px" Width="400px" InitialExpandDepth="0" InitialDataBindDepth="0" OnNodePopulate="WebDataTree1_NodePopulate"> <DataBindings> <ig:DataTreeNodeBinding DataMember="Test" TextField="Text" ValueField="Id" /> <ig:DataTreeNodeBinding DataMember="Emploeyees" TextField="LastName" ValueField="EmployeeID" /> <ig:DataTreeNodeBinding DataMember="EmployeeTerritories" TextField="TerritoryID" ValueField="TerritoryID" /> <ig:DataTreeNodeBinding DataMember="Orders" TextField="OrderID" ValueField="OrderID" /> </DataBindings> </ig:WebDataTree>
Code behind:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataSet data = GetData(); WebDataTree1.DataSource = data; WebDataTree1.DataBind(); } }
protected void WebDataTree1_NodePopulate(object sender, DataTreeNodeEventArgs e) { WebDataTree1.DataSource = GetData(); }
private DataSet GetData() { string connString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
string selectEmploeyeesSql = "SELECT * FROM Employees"; string selectEmployeeTerritoriesSql = "SELECT * FROM EmployeeTerritories";
DataSet data = new DataSet();
DataTable tableTest = new DataTable("Test"); tableTest.Columns.Add("Id", typeof(int)); tableTest.Columns.Add("Text", typeof(string));
tableTest.Rows.Add(1, "test 1"); tableTest.Rows.Add(2, "test 2");
SqlDataAdapter adapter = new SqlDataAdapter(selectEmploeyeesSql, connString); DataTable tableEmploeyees = new DataTable("Emploeyees"); adapter.Fill(tableEmploeyees);
tableEmploeyees.PrimaryKey = new DataColumn[] { tableEmploeyees.Columns["EmployeeId"] };
adapter = new SqlDataAdapter(selectEmployeeTerritoriesSql, connString); DataTable tableEmployeeTerritories = new DataTable("EmployeeTerritories"); adapter.Fill(tableEmployeeTerritories);
data.Tables.Add(tableTest); data.Tables.Add(tableEmploeyees); data.Tables.Add(tableEmployeeTerritories);
DataColumn parentColumn = tableEmploeyees.Columns["EmployeeId"]; DataColumn childColumn = tableEmployeeTerritories.Columns["EmployeeId"];
data.Relations.Add(parentColumn, childColumn);
return data; }
Thanks. This did the trick. I was missing the markup for the DataBindings stuff.
I have a similar problem. I have a heirarchical dataset with parent-child relationships but I can't get the tree to show the correct child rows.
The tree display the top parent table okay, with [+] expand icons next to the correct nodes but when I expand the nodes, each one contains nodes from the parent, not the child.
Tree code:
<ig:WebDataTree ID="wdtTemplateStructure" runat="server" DataSourceID="WebHierarchicalDataSource1" Height="450px" InitialDataBindDepth="0" InitialExpandDepth="0" Width="100%" onnodepopulate="wdtTemplateStructure_NodePopulate"> <DataBindings> <ig:DataTreeNodeBinding DataMember="Templates" TextField="DisplayText" ValueField="Code" /> <ig:DataTreeNodeBinding DataMember="Groups" TextField="DisplayText" ValueField="Code" /> </DataBindings> </ig:WebDataTree>
protected void Page_Load(object sender, EventArgs e){ if (!IsPostBack) { DSTemplateHeirarchy templateStructure = GetData(); wdtTemplateStructure.DataSourceID = null; wdtTemplateStructure.DataSource = templateStructure; wdtTemplateStructure.DataBind(); }}protected void wdtTemplateStructure_NodePopulate(object sender, Infragistics.Web.UI.NavigationControls.DataTreeNodeEventArgs e){ wdtTemplateStructure.DataSource = GetData();}private DSTemplateHeirarchy GetData(){ // Load data from business layer.}
DS Schema:-
<?xml version="1.0" standalone="yes"?><xs:schema id="DSTemplateHeirarchy" targetNamespace="http://tempuri.org/DSTemplateHeirarchy.xsd" xmlns:mstns="http://tempuri.org/DSTemplateHeirarchy.xsd" xmlns="http://tempuri.org/DSTemplateHeirarchy.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified"> <xs:element name="DSTemplateHeirarchy" msdata:IsDataSet="true" msdata:Locale="en-GB"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="Templates"> <xs:complexType> <xs:sequence> <xs:element name="ID" type="xs:int" minOccurs="0" /> <xs:element name="Code" type="xs:string" minOccurs="0" /> <xs:element name="DisplayText" type="xs:string" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="Groups"> <xs:complexType> <xs:sequence> <xs:element name="GroupID" msdata:Caption="ID" type="xs:int" minOccurs="0" /> <xs:element name="JobTemplateID" type="xs:int" minOccurs="0" /> <xs:element name="Code" type="xs:string" minOccurs="0" /> <xs:element name="OrderNo" type="xs:int" minOccurs="0" /> <xs:element name="DisplayText" type="xs:string" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> </xs:element> <xs:annotation> <xs:appinfo> <msdata:Relationship name="Templates_Groups" msdata:parent="Templates" msdata:child="Groups" msdata:parentkey="ID" msdata:childkey="JobTemplateID" /> </xs:appinfo> </xs:annotation></xs:schema>
Got it. I needed primary keys in the DS. All working now.