Jump to content

Populate TreeView from DataBase

- - - - -

  • Please log in to reply
8 replies to this topic

#1
DenKain

DenKain

    Newbie

  • Members
  • PipPip
  • 29 posts
So no matter what I do I can’t get this concept to work. What I am trying to do is process some database information in a class and then have it placed in a TreeView. The columns in the DB are, at least the ones that this is concerned with, Year, Month, entryTitle. So far I can get the year to display correctly as the root node. My issue is the month nodes. The way I am doing it populates one month node per entry (like a journal entry).

So In the end I am looking for something like:

2005
--August
----My birthday
2010
--January
----New years eve




using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Finisar.SQLite;
using System.Collections;

namespace myNameSpace
{
    class classTreeListFromSqlite
    {
        public static TreeView fillTree(TreeView foo)
        {
            #region dbConnectAndFill
            //SQLite Connection Info
            SQLiteConnection sql_con;
            SQLiteCommand sql_cmd;
            SQLiteDataAdapter DB;
            DataSet DS = new DataSet();

            sql_con = new SQLiteConnection
                ("Data Source=test.db;Version=3;New=False;Compress=True;");

            sql_con.Open();
            sql_cmd = sql_con.CreateCommand();
            string CommandText = "select * From User_Journals Order By ID desc";
            DB = new SQLiteDataAdapter(CommandText, sql_con);
            DS.Reset();
            DB.Fill(DS);
            sql_con.Close(); 
            #endregion

            #region sortYearNodes
            //Start Sorting the years and placing
            //them into the TreeView as Nodes
            ArrayList tempYearList = new ArrayList();
            ArrayList yearList = new ArrayList();
            List<DataRow> dataList = new List<DataRow>();

            foreach (DataRow theRow in DS.Tables[0].Rows)
            {
                tempYearList.Add(theRow["year"].ToString());

            }

            tempYearList.Sort();

            for (int i = 1; i <= tempYearList.Count - 1; i++)
            {
                object o = tempYearList[i];

                if (yearList.Contains(o))
                {
                }
                else
                {
                    yearList.Add(o);
                }
            }

            int x = 0;
            foreach (string yearString in yearList)
            {

                TreeNode yearNod = new TreeNode();
                yearNod.Text = yearList[x].ToString();
                foo.Nodes.Add(yearNod);
                x++;
            } 
            #endregion

            #region sortMonthNodes
            //Start sorting the months
            ArrayList tempMonthList = new ArrayList();
            ArrayList monthList = new ArrayList();

            foreach (DataRow theRow in DS.Tables[0].Rows)
            {
                tempMonthList.Add(theRow["month"].ToString());
            }

            tempMonthList.Sort();

            for (int i = 0; i <= tempMonthList.Count - 1; i++)
            {
                object o = tempMonthList[i];

                if (monthList.Contains(o))
                {
                }
                else
                {
                    monthList.Add(o);
                }
            }

            for (int i = 0; i < monthList.Count; i++)
            {

                //Start Connect to DB
                SQLiteCommand sql_month_cmd;
                SQLiteDataAdapter month_DB;
                DataSet month_DS = new DataSet();

                sql_con.Open();
                sql_month_cmd = sql_con.CreateCommand();
                string month_CommandText = "SELECT year FROM User_Journals WHERE month = " + monthList[i] + "";
                month_DB = new SQLiteDataAdapter(month_CommandText, sql_con);
                month_DS.Reset();
                month_DB.Fill(month_DS);
                sql_con.Close();
                //End Connect to DB

                foreach (DataRow theRow in month_DS.Tables[0].Rows)
                {
                    object o = theRow["year"].ToString();

                    #region monthNumericToEnglish
                        switch (monthList[i].ToString())
                        {
                            case "1":
                                monthList[i] = "January";
                                break;

                            case "2":
                                monthList[i] = "February";
                                break;

                            case "3":
                                monthList[i] = "March";
                                break;

                            case "4":
                                monthList[i] = "April";
                                break;

                            case "5":
                                monthList[i] = "May";
                                break;

                            case "6":
                                monthList[i] = "June";
                                break;

                            case "7":
                                monthList[i] = "July";
                                break;

                            case "8":
                                monthList[i] = "August";
                                break;

                            case "9":
                                monthList[i] = "September";
                                break;

                            case "10":
                                monthList[i] = "October";
                                break;

                            case "11":
                                monthList[i] = "November";
                                break;

                            case "12":
                                monthList[i] = "December";
                                break;
                        }
                    #endregion

                    int yearListIndex = yearList.IndexOf(o);

                    TreeNode monthNod = new TreeNode();
                    monthNod.Text = monthList[i].ToString();

                    //MessageBox.Show("Contains: " + foo.Nodes[yearListIndex].Nodes.Contains(monthNod));

                    if(foo.Nodes[yearListIndex].Nodes.Contains(monthNod).ToString() == "False")
                    {
                    
                    }
                    else
                    {
                    }

                    foo.Nodes[yearListIndex].Nodes.Add(monthNod);

                    MessageBox.Show("Contains: " + foo.Nodes[yearListIndex].Nodes.Contains(monthNod));

                }
            }



            #endregion

            foo.ExpandAll();
            return foo;
        }

    }
}






#2
DenKain

DenKain

    Newbie

  • Members
  • PipPip
  • 29 posts
So, since the reply to my post that I was replying to has been deleted can a mod delete this reply?

Edited by DenKain, 21 July 2010 - 08:57 AM.
the reply I replied to is gone


#3
DenKain

DenKain

    Newbie

  • Members
  • PipPip
  • 29 posts
bump

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Can't you do SELECT * FROM User_Journals ORDER BY year, month DESC ?

And then
TreeNode yearNode, monthNode;
foreach (DataRow row in DS.Tables[0].Rows)
{
  if( yearNode != null && yearNode.Text.equals(row["year"]) )
    if( monthNode != null && monthNode.Text.equals(row["month"]) )
      monthNode.add( new TreeNode(row["entryTitle"]) );
    else{
      monthNode = new TreeNode( row["month"] ); 
      yearNode.add(monthNode);      
      monthNode.add( new TreeNode(row["entryTitle"]) );
    }
  else{
   yearNode = new TreeNode( row["year"] );
   treeView.add(yearNode); 
   monthNode = new TreeNode( row["month"] ); 
   yearNode.add(monthNode);      
   monthNode.add( new TreeNode(row["entryTitle"]) );
  }

}


#5
DenKain

DenKain

    Newbie

  • Members
  • PipPip
  • 29 posts
Because that method creates multiple root nodes per entry. Instead of having something like:

Quote

2010
-June
--My Birthday
--The Day after
-July
--4th Picnic
2011
-January
--The New Year
I get this:

Quote

2010
-June
--My Birthday
2010
-June
--The Day after
2010
-July
--4th Picnic
2011
-January
--The New Year


#6
DenKain

DenKain

    Newbie

  • Members
  • PipPip
  • 29 posts
bump

Sorry for bumping so soon but I am suuuuper excited that I am almost finished with this project.

#7
DenKain

DenKain

    Newbie

  • Members
  • PipPip
  • 29 posts
bump

#8
DenKain

DenKain

    Newbie

  • Members
  • PipPip
  • 29 posts
bump

#9
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
Take a look at this and see if that helps.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users