Answered by:
how to add treenode from backend for each year

Question
-
ow to create Treenode for each year coming from databse
i have 2 years in my database 2011 n 2012
for each yr 2011 i have title,content ,date
yr 2012 i have title ,content,date
but 2011 contents month JAN,FEB ,MAR ,APR n 2012 contents month Dec ,Nov ,Mar ,Apr
i am able to create 2 treenodes by taking values from database but the month march of 2012 is getting added on 2011 only
i want 2011 to have JAN,FEB ,MAR ,APR n 2012 has separate Dec ,Nov ,Mar ,Apr
but my code is adding march month in treenode 2011 plz help me to solve issue thnx
Dim news_database As New NewsSchemaTableAdapter Dim news As New DataTable Dim new_date As Date Dim month As String Dim Latest_date As Integer news = news_database.GetDataByDate Dim news_row As DataRow = news.Rows(0) new_date = news_row("newsec_date") month = array(new_date.Month - 1) Date_Label.Text = new_date.Day & " " & month & ", " & new_date.Year Title_Label.Text = news_row("newsec_title") 'To split the sentence Dim s As String = news_row("newsec_body") Dim words As String() = s.Split(New Char() {"."}) Dim word As String ' Debug.WriteLine(words.Length()) For Each word In words Body_Label.Controls.Add(HtmlHelper.GetLiteral("<div><p>" & word & "</p></div> ")) Next ' Body_Label.Text = news_row("newsec_body") Dim id As String = news_row("newsec_id") Latest_date = new_date.Month ' Debug.WriteLine(Latest_date) If Not IsPostBack Then Dim yr As String = String.Empty For Each dr As DataRow In news.Rows Dim parentnode As TreeNode = Nothing yr = CDate(dr("newsec_date")).Year.ToString Debug.WriteLine(yr) parentnode = Treeview1.FindNode(yr) If parentnode Is Nothing Then Debug.WriteLine("Is Nothing") parentnode = New TreeNode(yr, yr) new_date = dr("newsec_date") month = array(new_date.Month - 1) Dim child As New TreeNode(month, month) parentnode.Collapse() Select Case new_date.Month Case 1 Dim grandchildnode As New TreeNode(dr("newsec_title"), dr("newsec_id")) jan.ChildNodes.Add(grandchildnode) parentnode.ChildNodes.Add(jan) jan.Collapse() Case 2 Dim grandchildnode As New TreeNode(dr("newsec_title"), dr("newsec_id")) feb.ChildNodes.Add(grandchildnode) parentnode.ChildNodes.Add(feb) feb.Collapse() Case 3 Dim grandchildnode As New TreeNode(dr("newsec_title"), dr("newsec_id")) mar.ChildNodes.Add(grandchildnode) parentnode.ChildNodes.Add(mar) mar.Collapse() Case 4 Dim grandchildnode As New TreeNode(dr("newsec_title"), dr("newsec_id")) apr.ChildNodes.Add(grandchildnode) parentnode.ChildNodes.Add(apr) apr.Collapse() Case 5 Dim grandchildnode As New TreeNode(dr("newsec_title"), dr("newsec_id")) may.ChildNodes.Add(grandchildnode) parentnode.ChildNodes.Add(may) may.Collapse() Case 6 Dim grandchildnode As New TreeNode(dr("newsec_title"), dr("newsec_id")) jun.ChildNodes.Add(grandchildnode) parentnode.ChildNodes.Add(jun) jun.Collapse() Case 7 Dim grandchildnode As New TreeNode(dr("newsec_title"), dr("newsec_id")) jul.ChildNodes.Add(grandchildnode) parentnode.ChildNodes.Add(jul) jul.Collapse() Case 8 Dim grandchildnode As New TreeNode(dr("newsec_title"), dr("newsec_id")) aug.ChildNodes.Add(grandchildnode) parentnode.ChildNodes.Add(aug) aug.Collapse() Case 9 Dim grandchildnode As New TreeNode(dr("newsec_title"), dr("newsec_id")) sep.ChildNodes.Add(grandchildnode) parentnode.ChildNodes.Add(sep) sep.Collapse() Case 10 Dim grandchildnode As New TreeNode(dr("newsec_title"), dr("newsec_id")) oct.ChildNodes.Add(grandchildnode) parentnode.ChildNodes.Add(oct) oct.Collapse() Case 11 Dim grandchildnode As New TreeNode(dr("newsec_title"), dr("newsec_id")) nov.ChildNodes.Add(grandchildnode) parentnode.ChildNodes.Add(nov) nov.Collapse() Case 12 Dim grandchildnode As New TreeNode(dr("newsec_title"), dr("newsec_id")) dec.ChildNodes.Add(grandchildnode) parentnode.ChildNodes.Add(dec) dec.Collapse() End Select Treeview1.Nodes.Add(parentnode) Else new_date = dr("newsec_date") month = array(new_date.Month - 1) Dim child As New TreeNode(month, month) parentnode.Collapse() Select Case new_date.Month Case 1 Dim grandchildnode As New TreeNode(dr("newsec_title"), dr("newsec_id")) jan.ChildNodes.Add(grandchildnode) parentnode.ChildNodes.Add(jan) jan.Collapse() Case 2 Dim grandchildnode As New TreeNode(dr("newsec_title"), dr("newsec_id")) feb.ChildNodes.Add(grandchildnode) parentnode.ChildNodes.Add(feb) feb.Collapse() Case 3 Dim grandchildnode As New TreeNode(dr("newsec_title"), dr("newsec_id")) mar.ChildNodes.Add(grandchildnode) parentnode.ChildNodes.Add(mar) mar.Collapse() Case 4 Dim grandchildnode As New TreeNode(dr("newsec_title"), dr("newsec_id")) apr.ChildNodes.Add(grandchildnode) parentnode.ChildNodes.Add(apr) apr.Collapse() Case 5 Dim grandchildnode As New TreeNode(dr("newsec_title"), dr("newsec_id")) may.ChildNodes.Add(grandchildnode) parentnode.ChildNodes.Add(may) may.Collapse() Case 6 Dim grandchildnode As New TreeNode(dr("newsec_title"), dr("newsec_id")) jun.ChildNodes.Add(grandchildnode) parentnode.ChildNodes.Add(jun) jun.Collapse() Case 7 Dim grandchildnode As New TreeNode(dr("newsec_title"), dr("newsec_id")) jul.ChildNodes.Add(grandchildnode) parentnode.ChildNodes.Add(jul) jul.Collapse() Case 8 Dim grandchildnode As New TreeNode(dr("newsec_title"), dr("newsec_id")) aug.ChildNodes.Add(grandchildnode) parentnode.ChildNodes.Add(aug) aug.Collapse() Case 9 Dim grandchildnode As New TreeNode(dr("newsec_title"), dr("newsec_id")) sep.ChildNodes.Add(grandchildnode) parentnode.ChildNodes.Add(sep) sep.Collapse() Case 10 Dim grandchildnode As New TreeNode(dr("newsec_title"), dr("newsec_id")) oct.ChildNodes.Add(grandchildnode) parentnode.ChildNodes.Add(oct) oct.Collapse() Case 11 Dim grandchildnode As New TreeNode(dr("newsec_title"), dr("newsec_id")) nov.ChildNodes.Add(grandchildnode) parentnode.ChildNodes.Add(nov) nov.Collapse() Case 12 Dim grandchildnode As New TreeNode(dr("newsec_title"), dr("newsec_id")) dec.ChildNodes.Add(grandchildnode) parentnode.ChildNodes.Add(dec) dec.Collapse() End Select End If parentnode.Expand() Next
Thursday, September 27, 2012 9:48 AM
Answers
-
SS,
I have created a "simulation" of what you're trying to do - create TreeNodes showing the dates in logical order. In mine I'm creating a List(Of Date) and putting 15 randomly selected dates in it, then using a bit of LINQ to help do the rest.
I have the code on a page of my website here and following shows the result:
I hope that might give you some ideas of you might do this. :)Please call me Frank :)
- Proposed as answer by Mark Liu-lxf Friday, September 28, 2012 2:48 AM
- Marked as answer by Mark Liu-lxf Monday, October 8, 2012 1:00 PM
Thursday, September 27, 2012 2:53 PM