craskin
asked on
creating a threaded datagrid
this is more of a conceptual question at first that will probably get to practical examples later. what i'm trying to do is very much like what you see on a message board application, where you have categories with topics underneath and threads inside each topic. i want to create a simple navigation for this that is something like a treeview, but i'd rather not create an xml file for the menu. i'm hoping there is a way to use a nested datagrid of sorts so that i can take a table with information like
ID Name ParentID
1 abc 0
2 def 0
3 bfr 1
4 ghy 1
5 ius 2
and turn it into
abc
> bfr
> ghy
def
> ius
if this table structure is not the most efficient for what i want to do, what would be? and how would i go about making a nested datagrid of this type? if i can get to the point of the top level categories remaining in their own rows with the subcategories taking up another row, i can probably figure out a way to indent them on my own, however, i'd like it to also be collapsible, again like a treeview. if a treeview is what i should use, how do i bind it to a datatable?
ID Name ParentID
1 abc 0
2 def 0
3 bfr 1
4 ghy 1
5 ius 2
and turn it into
abc
> bfr
> ghy
def
> ius
if this table structure is not the most efficient for what i want to do, what would be? and how would i go about making a nested datagrid of this type? if i can get to the point of the top level categories remaining in their own rows with the subcategories taking up another row, i can probably figure out a way to indent them on my own, however, i'd like it to also be collapsible, again like a treeview. if a treeview is what i should use, how do i bind it to a datatable?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok, so i've followed that example and i have
Private Sub FillCategories(ByVal parent As TreeNode)
Dim dsCategories As DataSet = objCicada.GetCategories()
For Each row As DataRow In dsCategories.Tables(0).Row s
Dim node As New TreeNode
node.Text = row("CategoryName").ToStri ng
node.Value = row("CategoryID").ToString
node.PopulateOnDemand = True
node.SelectAction = TreeNodeSelectAction.Selec tExpand
parent.ChildNodes.Add(node )
Next
End Sub
Private Sub FillTopics(ByVal parent As TreeNode)
Dim dsTopics As DataSet = objCicada.GetSubCategories (parent.Va lue)
For Each row As DataRow In dsTopics.Tables(0).Rows
Dim node As New TreeNode
node.Text = row("CategoryName").ToStri ng
node.Value = row("CategoryID").ToString
node.PopulateOnDemand = False
node.SelectAction = TreeNodeSelectAction.Selec tExpand
parent.ChildNodes.Add(node )
Next
End Sub
Protected Sub TreeView1_TreeNodePopulate (ByVal Sender As Object, ByVal e As TreeNodeEventArgs)
Select Case e.Node.Depth
Case 0
FillCategories(e.Node)
Case 1
FillTopics(e.Node)
End Select
End Sub
and the control looks like
<asp:TreeView ID="TreeView1" OnTreeNodePopulate="TreeVi ew1_TreeNo dePopulate " runat="server"/>
but it doesn't render anything. i feel like i'm missing something from the treeview control to actuall fire it, but i dunno what that would be as i've never worked with a treeview before.
Private Sub FillCategories(ByVal parent As TreeNode)
Dim dsCategories As DataSet = objCicada.GetCategories()
For Each row As DataRow In dsCategories.Tables(0).Row
Dim node As New TreeNode
node.Text = row("CategoryName").ToStri
node.Value = row("CategoryID").ToString
node.PopulateOnDemand = True
node.SelectAction = TreeNodeSelectAction.Selec
parent.ChildNodes.Add(node
Next
End Sub
Private Sub FillTopics(ByVal parent As TreeNode)
Dim dsTopics As DataSet = objCicada.GetSubCategories
For Each row As DataRow In dsTopics.Tables(0).Rows
Dim node As New TreeNode
node.Text = row("CategoryName").ToStri
node.Value = row("CategoryID").ToString
node.PopulateOnDemand = False
node.SelectAction = TreeNodeSelectAction.Selec
parent.ChildNodes.Add(node
Next
End Sub
Protected Sub TreeView1_TreeNodePopulate
Select Case e.Node.Depth
Case 0
FillCategories(e.Node)
Case 1
FillTopics(e.Node)
End Select
End Sub
and the control looks like
<asp:TreeView ID="TreeView1" OnTreeNodePopulate="TreeVi
but it doesn't render anything. i feel like i'm missing something from the treeview control to actuall fire it, but i dunno what that would be as i've never worked with a treeview before.
ASKER
nevermind, i found it. it needed a node to start with, which is odd. so now it has a -1 level node with the 0 level categories underneath.
You're right, treeview control needs a startup node, it's usually the root node. I also found extremely useful not to populate the entire treeview, especially if you have more than two levels of hierarchial data and populate only those treenodes that user clicks on, 'just-in-time'.
ASKER