Adding database fields to the treeview

  • Thread starter Thread starter mmatin2411
  • Start date Start date
M

mmatin2411

Guest
Hi,

I have database table GroupFilter which contains Groups. Groups can be nested freely. For example:

Group1

Group 1-1

Group 1-2

Group 1-3

etc...

Group2

Group 2-1

Group 2-2

etc...

etc..

I have problem with nesting selected groups in the treeview.

GroupFilter table looks like

GroupFilterID | GroupFilterID_Parent | GroupFilterName

This is my code so far. I can nest Groups to the second level, but after that I'm stuck. GroupFilterID_Parent -1 is a Root.


Private Sub SelectGroupItems()
TreeViewGroup.Nodes.Clear()
Try
ConnectServer()
TreeViewGroup.Nodes.Add("Root (0)")
dbConnection.sqlCmd = New SqlCommand("select GroupFilterName, GroupFilterID, GroupFilterID_Parent from " & dbConnString & ".dbo.GroupFilter where FunctionID = @P1 order by GroupFilterID_Parent", dbConnServer)
dbConnection.sqlCmd.Parameters.Add("@P1", SqlDbType.Int)
dbConnection.sqlCmd.Parameters("@P1").Value = GroupFunctionID
dbConnection.sqlCmd.CommandTimeout = 0
dr = dbConnection.sqlCmd.ExecuteReader
Do While dr.Read = True
GroupPerentID = dr(2).ToString
If GroupPerentID = -1 Then
TreeViewGroup.Nodes(0).Nodes.Add(dr(0).ToString & " (" & dr(1).ToString & ")")
Else
Dim NewGroupID As String
For Each myNode As TreeNode In TreeViewGroup.Nodes.Item(0).Nodes
NewGroupID = Mid(myNode.Text, InStr(myNode.Text, "(") + 1, InStr(myNode.Text, ")") - (InStr(myNode.Text, "(") + 1))
If NewGroupID = GroupPerentID Then
TreeViewGroup.Nodes.Item(0).Nodes.Item(myNode.Index).Nodes.Add(dr(0).ToString & " (" & dr(1).ToString & ")")
Else
End If
Next
End If
Loop
dr.Close()
DisconnectServer()
Catch ex As Exception
DisconnectServer()
MsgBox(ex.Message & vbCrLf & ex.ToString, vbCritical)
End Try
End Sub
Mario

Continue reading...
 
Back
Top