Adding Items to TreeView & ListView from Database.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    Adding Items to TreeView & ListView from Database.

    This sample code displays employee name in the treeview control from the emp table of Scott schema in oracle database.

    To start with

    Select Microsoft windows common controls 6.0 (SP6) from components
    Add a TreeView and a ListView control to the form.

    Add this sample code to the form
    =============== ===========
    [code=vb]Dim con As New ADODB.Connectio n
    Dim rs As New ADODB.Recordset
    Dim rs1 As New ADODB.Recordset

    Private Sub Form_Load()
    con.Open "Provider=OraOL EDB.Oracle.1;Pa ssword=tiger;Pe rsist Security Info=True;User ID=scott;Data Source=das"

    call TREE
    End Sub

    Public Sub TREE()
    tv1.Visible = False
    tv1.Nodes.Clear
    tv1.Visible = True
    Dim scontactname As String
    Dim currentalpha As String

    rs1.Open "select * from emp order by ename", con, adOpenDynamic, adLockOptimisti c
    If (rs1.RecordCoun t > 0) Then
    rs1.MoveFirst
    End If
    For indx = Asc("A") To Asc("Z")
    currentalpha = Chr(indx)
    Set contactnode = tv1.Nodes.Add(, , currentalpha, currentalpha)
    If (Not rs1.EOF) Then
    Do While UCase(Left(rs1! ename, 1)) = currentalpha
    With rs1
    If (Not IsNull(!ename)) Then
    scontactname = !ename
    End If
    End With
    DoEvents
    Set contactnode = tv1.Nodes.Add(c urrentalpha, tvwChild, rs1!ename, scontactname)
    rs1.MoveNext
    If (rs1.EOF) Then
    Exit Do
    End If
    Loop
    End If
    Next

    rs1.Close
    DoEvents
    End Sub
    [/code]

    To display the details of the record as selected from the Treeview control.

    Add the column headers in the listview from property pallet as the field name from the table if desired.

    Add the following sample code
    =============== =========
    [code=vb]
    Private Sub tv1_NodeClick(B yVal Node As MSComctlLib.Nod e)
    LV1.ListItems.C lear
    rs.Open "select * from EMP where ENAME = '" & tv1.SelectedIte m.Key & "'", con, adOpenDynamic, adLockOptimisti c

    Dim li1 As ListItem
    Set li1 = LV1.ListItems.A dd()
    If Not rs.EOF Then

    If IsNull(rs(0)) Then
    li1.Text = ""
    Else
    li1.Text = rs(0)
    End If

    If IsNull(rs(1)) Then
    LV1.ListItems(1 ).ListSubItems. Add , , ""
    Else
    LV1.ListItems(1 ).ListSubItems. Add , , rs(1)
    End If

    If IsNull(rs(2)) Then
    LV1.ListItems(1 ).ListSubItems. Add , , ""
    Else
    LV1.ListItems(1 ).ListSubItems. Add , , rs(2)
    End If

    If IsNull(rs(3)) Then
    LV1.ListItems(1 ).ListSubItems. Add , , ""
    Else
    LV1.ListItems(1 ).ListSubItems. Add , , rs(3)
    End If
    If IsNull(rs(4)) Then
    LV1.ListItems(1 ).ListSubItems. Add , , ""
    Else
    LV1.ListItems(1 ).ListSubItems. Add , , rs(4)
    End If
    If IsNull(rs(5)) Then
    LV1.ListItems(1 ).ListSubItems. Add , , ""
    Else
    LV1.ListItems(1 ).ListSubItems. Add , , rs(5)
    End If
    If IsNull(rs(6)) Then
    LV1.ListItems(1 ).ListSubItems. Add , , ""
    Else
    LV1.ListItems(1 ).ListSubItems. Add , , rs(6)
    End If
    If IsNull(rs(7)) Then
    LV1.ListItems(1 ).ListSubItems. Add , , ""
    Else
    LV1.ListItems(1 ).ListSubItems. Add , , rs(7)
    End If

    End If
    rs.Close
    End Sub
    [/code]
Working...