Hi,
I'm having difficulty trying to build a multi level hierarchy that has unknown levels.
I've read loads and loads of examples but they all assume the number of levels are known. However, in my case I may have "x" number of nodes, which may have "x" number of child nodes, which may have "x" number of grandchild nodes etc etc.
My data is drawn from an SQL database where I use the SQL 2008 HierarchyID data type to represent my hierarchy in the database.
I've built the following class for my products:
I then have the following method to download data from the database which I need to some how construct an ObservableCollection() which contains nodes "x" number of levels deep. Please note that some nodes may be 2 levels deep, others may be 3 levels deep, so forth and so forth.
I've found a post that discusses unlimited nodes, http://www.telerik.com/community/forums/wpf/treeview/templated-node-multiple-parent.aspx , but in the example given it has the following lines of code to build up unlimited levels:
Surely this is only valid providing you know the number of levels before runtime?
I guess I'm essentially looking for a generic hierarchy builder where the number of levels are unknown until runtime, i.e. when data is download from database.
Any help would be greatly appreciated.
Thanks for your time.
I'm having difficulty trying to build a multi level hierarchy that has unknown levels.
I've read loads and loads of examples but they all assume the number of levels are known. However, in my case I may have "x" number of nodes, which may have "x" number of child nodes, which may have "x" number of grandchild nodes etc etc.
My data is drawn from an SQL database where I use the SQL 2008 HierarchyID data type to represent my hierarchy in the database.
I've built the following class for my products:
public class Products : INotifyPropertyChanged, IDataErrorInfo{private Int64 m_ID;private SqlHierarchyId m_Hierarchy;private string m_Name;private Int16 m_Level;private ObservableCollection<Products> m_ChildProducts;// Default Constructorpublic Products(){ChildProducts = new ObservableCollection<Products>();}//Propertiespublic Int64 ID{get{return m_ID;}set{m_ID = value;OnPropertyChanged(new PropertyChangedEventArgs("ID"));}}public SqlHierarchyId Hierarchy{get{return m_Hierarchy;}set{m_Hierarchy = value;OnPropertyChanged(new PropertyChangedEventArgs("Hierarchy"));}}public Int16 Level{get{return m_Level;}set{m_Level = value;OnPropertyChanged(new PropertyChangedEventArgs("Level"));}}public String Name{get{return m_Name;}set{m_Name = value;OnPropertyChanged(new PropertyChangedEventArgs("Name"));}}public ObservableCollection<Products> ChildProducts{get{return m_ChildProducts;}set{m_ChildProducts = value;OnPropertyChanged(new PropertyChangedEventArgs("ChildProducts"));}}//INotifyPropertyChanged Eventpublic event PropertyChangedEventHandler PropertyChanged;public void OnPropertyChanged(PropertyChangedEventArgs e){if (PropertyChanged != null)PropertyChanged(this, e);}}I then have the following method to download data from the database which I need to some how construct an ObservableCollection() which contains nodes "x" number of levels deep. Please note that some nodes may be 2 levels deep, others may be 3 levels deep, so forth and so forth.
public static ObservableCollection<Products> GetProductsHierarchy() { ObservableCollection<Products> products = new ObservableCollection<Products>(); SqlConnection connection = new SqlConnection(DBConnection.GetConnection().ConnectionString); string selectStatement = "SELECT ID, Hierarchy, Name " + "FROM SpecProducts " + "WHERE (EnableDisable IS NULL)" + "ORDER BY Hierarchy"; SqlCommand selectCommand = new SqlCommand(selectStatement, connection); try { connection.Open(); SqlDataReader reader = selectCommand.ExecuteReader(); while (reader.Read()) { Products product = new Products(); product.ID = (Int64)reader["ID"]; product.Name = reader["Name"].ToString(); product.Hierarchy = (SqlHierarchyId)reader["Hierarchy"]; product.Level = (Int16)product.Hierarchy.GetLevel(); //**** How to create recursive loop to add unknown levels to nodes???? **** products.Add(product); } return products; } catch (SqlException ex) { throw ex; } finally { connection.Close(); } }I've found a post that discusses unlimited nodes, http://www.telerik.com/community/forums/wpf/treeview/templated-node-multiple-parent.aspx , but in the example given it has the following lines of code to build up unlimited levels:
ObservableCollection<Node> nodes = new ObservableCollection<Node>();nodes.Add(new Node("Company"));nodes[0].Nodes.Add(new Node("Department"));nodes[0].Nodes[0].Nodes.Add(new Node("here are you"));nodes[0].Nodes[0].Nodes[0].Nodes.Add(new Node("even deeper"));radTreeView1.ItemsSource = nodes;Surely this is only valid providing you know the number of levels before runtime?
I guess I'm essentially looking for a generic hierarchy builder where the number of levels are unknown until runtime, i.e. when data is download from database.
Any help would be greatly appreciated.
Thanks for your time.