This is a migrated thread and some comments may be shown as answers.

[Solved] Display Hierarchical Data?

3 Answers 167 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Clayton Garrett
Top achievements
Rank 1
Clayton Garrett asked on 22 Sep 2008, 06:00 PM
I'd like to display hierarchical data in the combobox.  Any suggestions on how to accomplish this?

Example would be:

Home
About
   Mission Statement
   Vision
Programs
   Program 1
       Requirements
       Features
   Program 2

Any help is appreciated!

Thanks,
Jason

3 Answers, 1 is accepted

Sort by
0
Accepted
Serrin
Top achievements
Rank 1
answered on 22 Sep 2008, 07:19 PM
Hey Jason,

Looks like there are a few options you can go with on this...  You can put a TreeView into a combobox like in this example:

TreeView in Combo Box

Or you can do something like this example which takes data from an XML file...

<?xml version="1.0" encoding="utf-8" ?> 
<Menu> 
  <Home Value="1" /> 
  <About Value="2">  
    <MissionStatement Value="3" /> 
    <Vision Value="4" /> 
  </About> 
  <Programs Value="5">  
    <Program1 Value="6">  
      <Requirements Value="7"/>  
      <Features Value="8"/>  
    </Program1> 
    <Program2 Value="9"/>  
  </Programs> 
</Menu> 
 

And then use this code to populate the ComboBox:

    protected void Page_Load(object sender, EventArgs e)  
    {  
        if (!IsPostBack)  
        {  
            XmlDocument MenuDoc = new XmlDocument();  
            MenuDoc.Load(MapPath("xmlexample.xml"));  
            RecurseNodes(MenuDoc.DocumentElement);  
        }  
    }  
 
    private void RecurseNodes(XmlNode xmlNode)  
    {  
        // loop starts at level 0  
        RecurseNodes(xmlNode, 0);  
    }  
 
    private void RecurseNodes(XmlNode xmlNode, int level)  
    {  
        string s;  
        s = string.Format("{0} {1}", new string('-', level), xmlNode.Name);  
        RadComboBox1.Items.Add(new RadComboBoxItem(s.ToString()));  
 
        foreach (XmlNode menuChild in xmlNode.ChildNodes)  
        {  
            RecurseNodes(menuChild, level + 1);  
        }  
    }  
 

Thanks to the MS certification book for that second way to do it. :)

-Serrin
0
Clayton Garrett
Top achievements
Rank 1
answered on 22 Sep 2008, 07:42 PM
Hi Serrin,

Thanks for the quick reply!  I was trying to implement something more inline with the second solution you listed.  Any idea how I can incorporate that second solution with my code below:

       Private Sub GenerateComboBoxHierarchy() 
            Dim ds As DataSet = DBObjects.SiteMapNodeCollection.GetAllAsDataSet 
            ds.Relations.Add("NodeRelation", ds.Tables(0).Columns("SiteMapNodeId"), ds.Tables(0).Columns("ParentId")) 
 
            For Each dbRow As DataRow In ds.Tables(0).Rows 
                If dbRow.IsNull("ParentId") Then 
                    Dim node As RadComboBoxItem = CreateNode(dbRow("Title").ToString(), dbRow("PageContentId"), IIf(dbRow("ParentId") Is Convert.DBNull, 0, dbRow("ParentId"))) 
                    RecursivelyPopulate(dbRow, node) 
                End If 
            Next 
 
        End Sub 
 
        Private Sub RecursivelyPopulate(ByVal dbRow As DataRow, ByVal node As RadComboBoxItem) 
            For Each childRow As DataRow In dbRow.GetChildRows("NodeRelation") 
                Dim childNode As RadComboBoxItem = CreateNode(String.Concat(childRow("Title").ToString()), childRow("PageContentId"), childRow("ParentId")) 
                rcbExistingPages.Items.Add(childNode) 
                RecursivelyPopulate(childRow, childNode) 
            Next 
        End Sub 
 
        Private Function CreateNode(ByVal text As String, ByVal value As Integer, ByVal parentId As Integer) As RadComboBoxItem 
            Dim node As New RadComboBoxItem(text, value) 
            node.Attributes.Add("PageContentId", value) 
            Return node 
        End Function 

0
Clayton Garrett
Top achievements
Rank 1
answered on 22 Sep 2008, 07:49 PM
Serrin,

I think I figured it out:

Private Sub RecursivelyPopulate(ByVal dbRow As DataRow, ByVal node As RadComboBoxItem, ByVal level As Integer) 
            For Each childRow As DataRow In dbRow.GetChildRows("NodeRelation") 
                Dim s As String = String.Format("{0} {1}", New String(Chr(32), level), childRow("Title").ToString()) 
                Dim childNode As RadComboBoxItem = CreateNode(String.Concat(s), childRow("PageContentId"), childRow("ParentId")) 
                rcbExistingPages.Items.Add(childNode) 
                RecursivelyPopulate(childRow, childNode, level + 1) 
            Next 
End Sub 

Tags
ComboBox
Asked by
Clayton Garrett
Top achievements
Rank 1
Answers by
Serrin
Top achievements
Rank 1
Clayton Garrett
Top achievements
Rank 1
Share this question
or