Hallo, I want to generate my treeview base on the data in my table, for example i have a table TreeStructure that contain
ID -> auto_inc
PARENT_ID -> int
NAME -> character
ID is the primary key, PARENT_ID contains ID of it's parent and NAME is the text I want to display
I want to know is there any way i can generate this data into structured treeview automatically? because before this I do it manually with recursive.
Data Sample ( ID / PARENT_ID / NAME )
1 / 0 / IT , 2 / 0 / SUPPORT , 3 / 0 / SALES
4 / 1 / DEVELOPER , 4 / 1 / NETWORK , 5 / 2 / EDP
tree result :
- IT
---- DEVELOPER
---- NETWORK
- SUPPORT
----EDP
- HRD
thx before :)
Hello,
There are various ways to proceed with binding from a single self-referencing table with ID -> ParentID relation. Recursion is one of them, but there are also easier ways to do it. For example, you can use the Levels collection of the tree to denote the relation - there is a great article describing just that here:
http://devcenter.infragistics.com/Support/KnowledgeBaseArticle.aspx?ArticleID=10087
Another great way to do that, is to use our new WebHierarchicalDataSource control, which lets you create hierarchical relation with a single self-referencing table. We have a nice example that shows that here:
http://samples.infragistics.com/2008.2/webfeaturebrowser/default.htm
(just select the WebHierarchicalDataSource -> Self-Refrencing example from the navigational menu on the left) - full source code is provided there as well, e.g.
<ignav:UltraWebTree ID="UltraWebTree1" runat="server" DataSourceID="WebHierarchicalDataSource1" DefaultImage="" HoverClass="" Indentation="20" StyleSetName="LucidDream" BackColor="#FCFCFC" BorderColor="#E2F2FC" BorderStyle="Solid" BorderWidth="1px" Width="250px" EnableAppStyling="True" ExpandOnClick="True" SingleBranchExpand="True" InitialExpandDepth="2" > <DataBindings> <ignav:NodeBinding DataMember="AccessDataSource1_DefaultView" TextField="LastName" /> </DataBindings> </ignav:UltraWebTree> <aikido:WebHierarchicalDataSource ID="WebHierarchicalDataSource1" runat="server"> <DataRelations> <aikido:DataRelation ChildColumns="ReportsTo" ChildDataViewID="AccessDataSource1_DefaultView" ParentColumns="EmployeeID" ParentDataViewID="AccessDataSource1_DefaultView" /> </DataRelations> <DataViews> <aikido:DataView ID="AccessDataSource1_DefaultView" DataMember="DefaultView" DataSourceID="AccessDataSource1" /> </DataViews> </aikido:WebHierarchicalDataSource> <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/Nwind.mdb" SelectCommand="SELECT [EmployeeID], [LastName], [FirstName], [ReportsTo] FROM [Employees]"></asp:AccessDataSource>
Hope this helps.
Hallo Rumen
Thx for the fast reply, this is exactly what i'm looking for, i've tried http://devcenter.infragistics.com/Support/KnowledgeBaseArticle.aspx?ArticleID=10087 and it working just fine, thx for your support :)
========================================
I want to ask another questions, maybe it seems silly, before this i use TreeView component that ASP.NET provide,it store text and value when i construct it
TreeNode tn = new TreeNode(text, value);
value contains the ID, and text contains its NAME, so when i click the node, i can identify it from its value
If using UltraWebGrid i haven't found out in what property at Node should i put the ID value , i'm planning on using tag attributes, but somehow e.Node.DataItem always have null value :( .
protected void Tree_NodeBound(object sender, WebTreeNodeEventArgs e){ DataRowView drv = e.Node.DataItem as DataRowView;
e.Node.Tag = drv["ID"].ToString();
}
======================>>>>>
hehe, i already found out how, it can be done when declaring the column name, but i still don't know why e.Node.DataItem is null
for (int i = 0; i < 50; i++){ this.UltraWebTree1.Levels[i].ColumnName = "Name"; this.UltraWebTree1.Levels[i].RelationName = "r1"; this.UltraWebTree1.Levels[i].LevelKeyField = "ID";}
This is weird, DataItem should be set in NodeBound if you are using databinding... here is my code, could you please try something similar to that and see how it goes? I will also check the other two issues you are having with node editing and write back later.
protected void Page_Load(object sender, EventArgs e) { //retrieve the data DataSet ds = GetData(); //assign the datasource DataView dv = ds.Tables[0].DefaultView; dv.RowFilter = "ParentID=0"; this.UltraWebTree1.DataSource = dv; //set up the levels to use the relation for (int i = 0; i < 50; i++) { this.UltraWebTree1.Levels[i].ColumnName = "Name"; this.UltraWebTree1.Levels[i].RelationName = "r1"; } //databind this.UltraWebTree1.DataBind(); } //retrieve the data private DataSet GetData() { DataSet ds1 = new DataSet(); DataTable nodes = new DataTable(); System.Data.OleDb.OleDbCommand com1 = new System.Data.OleDb.OleDbCommand(); System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(); System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(); conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\db1.mdb;Persist Security Info=True"; com1.Connection = conn; com1.CommandText = "Select * from Table1"; da.SelectCommand = com1; da.Fill(nodes); ds1.Tables.Add(nodes); DataRelation dr1 = new DataRelation("r1", ds1.Tables[0].Columns["ID"], ds1.Tables[0].Columns["ParentID"], false); ds1.Relations.Add(dr1); return ds1; } protected void UltraWebTree1_NodeBound(object sender, WebTreeNodeEventArgs e) { DataRow row = e.Node.DataItem as DataRow; int rowIndex = row.Table.Rows.IndexOf(row); DataRowView rowView = row.Table.DefaultView[rowIndex]; e.Node.Tag = rowView["Name"]; }
Hallo Rumen,
About the code you gave me regarding to DataItem, I have try it exactly what you told me, and the result is the same
DataRow row = e.Node.DataItem as DataRow;
it still returns null, maybe there are some property in UltraWebTree that must be set?below this is a definition of my webtree
<ignav:UltraWebTree ID="tree" runat="server" Font-Names="verdana" Font-Size="8pt" DataKeyOnClient="true" AllowDrag="True" AllowDrop="True" Cursor="Hand" Editable="True" ExpandAnimation="None" InitialExpandDepth="1" WebTreeTarget="ClassicTree" DefaultImage="./img/ig_treeXPFolderClosed.GIF" DefaultSelectedImage="./img/ig_treeXPFolderOpen.gif" HiliteClass="" HoverClass="" Indentation="20" OnNodeAdded="Tree_NodeAdded" OnNodeBound="Tree_NodeBound" OnDataBound="Tree_DataBound" > <ClientSideEvents BeforeNodeUpdate="BeforeNodeUpdate" EditKeyUp="EditKeyUp" EditKeyDown="EditKeyDown" /> <Images> <RootNodeImage Url="./img/MyComputer.gif" /> <DefaultImage Url="./img/ig_treeXPFolderClosed.GIF" /> <SelectedImage Url="./img/ig_treeXPFolderOpen.gif" /> </Images> <NodeEditStyle Font-Names="Verdana" Font-Size="7pt" Height="100px" Cursor="Default"> <Margin Bottom="0px" Left="0px" /> </NodeEditStyle> </ignav:UltraWebTree>
Well, I really do not know, I am running this again and again and it is working for me. Could this be a version thing? I am trying with 8.1 release. In this case, maybe you can send the same solution to Developer Support and they can probably verify this and provide some suggestions.
hmm maybe, i'll post it at the support tomorrow, rumen thx for your support, i hope you're not bored helping me again some other time :)
Haloo Rumen,
I've tried version 8.2 and the DataItem works fine, I think it really is problems for version 7.2