recursive function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andreas Schubert

    #1

    recursive function

    Hello newsgroup,

    i need a recursive funtion to fill a treeviewstructu re.
    my table looks like this:

    category_id, Category, Parent

    where Parent points to category_id when it is a subentry.
    Now I use this to read all data of the table:

    Dim strSQL As String
    strSQL = "SELECT * FROM categories"
    Dim myConnection As SqlConnection
    myConnection = New SqlConnection(m _connStr)
    Dim mySqlDataAdapte r As SqlDataAdapter = New SqlDataAdapter( strSQL,
    myConnection)
    Dim myDataSet As DataSet = New DataSet
    Dim i As Integer
    mySqlDataAdapte r.Fill(myDataSe t, "Categories ")

    Now I need a function to enumerate through all the data in the dataset and
    add the nodes to the treeview and calling itself if subentries have to be
    entered.
    How do I do such a thing?
    I am an absolute newbie with .NET

    Thanks for your help

    Andreas


  • david

    #2
    Re: recursive function

    On 2005-09-17, Andreas Schubert <anwendungsprog rammierer.Remov eSpam@web.de> wrote:[color=blue]
    > Hello newsgroup,
    >
    > i need a recursive funtion to fill a treeviewstructu re.
    > my table looks like this:
    >
    > category_id, Category, Parent
    >
    > where Parent points to category_id when it is a subentry.
    > Now I use this to read all data of the table:
    >
    > Dim strSQL As String
    > strSQL = "SELECT * FROM categories"
    > Dim myConnection As SqlConnection
    > myConnection = New SqlConnection(m _connStr)
    > Dim mySqlDataAdapte r As SqlDataAdapter = New SqlDataAdapter( strSQL,
    > myConnection)
    > Dim myDataSet As DataSet = New DataSet
    > Dim i As Integer
    > mySqlDataAdapte r.Fill(myDataSe t, "Categories ")
    >
    > Now I need a function to enumerate through all the data in the dataset and
    > add the nodes to the treeview and calling itself if subentries have to be
    > entered.
    > How do I do such a thing?
    > I am an absolute newbie with .NET[/color]

    There's more efficient ways of doing this, but this untested example
    should more or less work...

    Private Sub FillLevel(ByVal nodes As TreeNodeCollect ion, _
    ByVal dt As DataTable, ByVal parentID As Integer)

    Dim rows() As DataRow = _
    dt.Select("Pare nt = " & parentID.ToStri ng(), "")
    For Each row As DataRow In rows
    Dim node As TreeNode = nodes.Add(row(" Category").ToSt ring())
    FillLevel(node. Nodes, dt, CInt(row("categ ory_id")))
    Next
    End Sub

    ' And kick it off with

    FillLevel(MyTre eView.Nodes, myDataSet.Table s(0), TOP_LEVEL_ID)

    where top_level_id is how you identify your top-level entries.


    Comment

    • Andreas Schubert

      #3
      Re: recursive function

      thanks david, will give it a try

      "david" <david@woofix.l ocal.dom> schrieb im Newsbeitrag
      news:slrndiojlr .ep3.david@loca lhost.localdoma in...[color=blue]
      > On 2005-09-17, Andreas Schubert
      > <anwendungsprog rammierer.Remov eSpam@web.de> wrote:[color=green]
      >> Hello newsgroup,
      >>
      >> i need a recursive funtion to fill a treeviewstructu re.
      >> my table looks like this:
      >>
      >> category_id, Category, Parent
      >>
      >> where Parent points to category_id when it is a subentry.
      >> Now I use this to read all data of the table:
      >>
      >> Dim strSQL As String
      >> strSQL = "SELECT * FROM categories"
      >> Dim myConnection As SqlConnection
      >> myConnection = New SqlConnection(m _connStr)
      >> Dim mySqlDataAdapte r As SqlDataAdapter = New SqlDataAdapter( strSQL,
      >> myConnection)
      >> Dim myDataSet As DataSet = New DataSet
      >> Dim i As Integer
      >> mySqlDataAdapte r.Fill(myDataSe t, "Categories ")
      >>
      >> Now I need a function to enumerate through all the data in the dataset
      >> and
      >> add the nodes to the treeview and calling itself if subentries have to be
      >> entered.
      >> How do I do such a thing?
      >> I am an absolute newbie with .NET[/color]
      >
      > There's more efficient ways of doing this, but this untested example
      > should more or less work...
      >
      > Private Sub FillLevel(ByVal nodes As TreeNodeCollect ion, _
      > ByVal dt As DataTable, ByVal parentID As Integer)
      >
      > Dim rows() As DataRow = _
      > dt.Select("Pare nt = " & parentID.ToStri ng(), "")
      > For Each row As DataRow In rows
      > Dim node As TreeNode = nodes.Add(row(" Category").ToSt ring())
      > FillLevel(node. Nodes, dt, CInt(row("categ ory_id")))
      > Next
      > End Sub
      >
      > ' And kick it off with
      >
      > FillLevel(MyTre eView.Nodes, myDataSet.Table s(0), TOP_LEVEL_ID)
      >
      > where top_level_id is how you identify your top-level entries.
      >
      >[/color]


      Comment

      • Andreas Schubert

        #4
        Re: recursive function

        thank you very much, works like a charm.

        Andy

        "david" <david@woofix.l ocal.dom> schrieb im Newsbeitrag
        news:slrndiojlr .ep3.david@loca lhost.localdoma in...[color=blue]
        > On 2005-09-17, Andreas Schubert
        > <anwendungsprog rammierer.Remov eSpam@web.de> wrote:[color=green]
        >> Hello newsgroup,
        >>
        >> i need a recursive funtion to fill a treeviewstructu re.
        >> my table looks like this:
        >>
        >> category_id, Category, Parent
        >>
        >> where Parent points to category_id when it is a subentry.
        >> Now I use this to read all data of the table:
        >>
        >> Dim strSQL As String
        >> strSQL = "SELECT * FROM categories"
        >> Dim myConnection As SqlConnection
        >> myConnection = New SqlConnection(m _connStr)
        >> Dim mySqlDataAdapte r As SqlDataAdapter = New SqlDataAdapter( strSQL,
        >> myConnection)
        >> Dim myDataSet As DataSet = New DataSet
        >> Dim i As Integer
        >> mySqlDataAdapte r.Fill(myDataSe t, "Categories ")
        >>
        >> Now I need a function to enumerate through all the data in the dataset
        >> and
        >> add the nodes to the treeview and calling itself if subentries have to be
        >> entered.
        >> How do I do such a thing?
        >> I am an absolute newbie with .NET[/color]
        >
        > There's more efficient ways of doing this, but this untested example
        > should more or less work...
        >
        > Private Sub FillLevel(ByVal nodes As TreeNodeCollect ion, _
        > ByVal dt As DataTable, ByVal parentID As Integer)
        >
        > Dim rows() As DataRow = _
        > dt.Select("Pare nt = " & parentID.ToStri ng(), "")
        > For Each row As DataRow In rows
        > Dim node As TreeNode = nodes.Add(row(" Category").ToSt ring())
        > FillLevel(node. Nodes, dt, CInt(row("categ ory_id")))
        > Next
        > End Sub
        >
        > ' And kick it off with
        >
        > FillLevel(MyTre eView.Nodes, myDataSet.Table s(0), TOP_LEVEL_ID)
        >
        > where top_level_id is how you identify your top-level entries.
        >
        >[/color]


        Comment

        Working...