DataTableTreeModel.cs :  » GUI » TreeViewAdv » SampleApp » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GUI » TreeViewAdv 
TreeViewAdv » SampleApp » DataTableTreeModel.cs
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using Aga.Controls.Tree;
using System.Drawing;
namespace SampleApp{
    /// <summary>
    /// Using a System.Data.DataTable to represent a tree structure 
    /// </summary>
  public class DataTableTreeModel :  TreeModelBase
  {
        private DataRowNode m_root;

        DataTable m_table;
        string m_IDColumnName;

    public DataTableTreeModel(DataTable table, string idColumnName)
    {
            m_table = table;
            m_IDColumnName = idColumnName;
            DataRow[] rows = table.Select(m_IDColumnName+" = ParentID");
            if( rows.Length ==0 )
            {
                throw new Exception("DataTableModel Requires a root Node");
            }
            m_root = new DataRowNode(rows[0],rows[0]["Name"].ToString());
            m_root.Row = rows[0];
    }

        public override System.Collections.IEnumerable GetChildren(TreePath treePath)
        {
            List<DataRowNode> items = new List<DataRowNode>();

            if (treePath.IsEmpty() )
            {
                items.Add(m_root);
            }
            else
            {
                DataRowNode n = treePath.LastNode as DataRowNode;

                DataRow row = n.Row;
                int id = Convert.ToInt32(row[m_IDColumnName]);

                DataRow[] rows = m_table.Select("ParentID = " + id+" and "+m_IDColumnName+" <> "+id);
                foreach (DataRow r in rows)
                {
                    DataRowNode node = new DataRowNode(r,r["Name"].ToString());
                    node.Row = r;
                    //SampleApp.Properties.Resources.ResourceManager.
                    //node.Icon = new Bitmap(SampleApp.Properties.Resources.Records,new Size(15,15));
                    items.Add(node);
                }
            }
            return items;
        }

        public override bool IsLeaf(TreePath treePath)
        {
            DataRowNode n = treePath.LastNode as DataRowNode;
            if (n.Row["IsFolder"] == DBNull.Value)
                return false;
            return !Convert.ToBoolean(n.Row["IsFolder"]);
        }


        //public event EventHandler<TreeModelEventArgs> NodesChanged;

         //public event EventHandler<TreeModelEventArgs> NodesInserted;

        //public event EventHandler<TreeModelEventArgs> NodesRemoved;

        //public event EventHandler<TreePathEventArgs> StructureChanged;


        public void AddChild(TreePath parent, string text)
        {
            DataRowNode n = parent.LastNode as DataRowNode;
             
           DataRow r =   m_table.NewRow();
           r["ID"] = GetNextID();
           r["ParentID"] = n.Row["ID"];
           r["IsFolder"] = false;
           r["Name"] = text;
           r["Tag"] = "";
           m_table.Rows.Add(r);
           DataRowNode child = new DataRowNode(r, text);
           OnStructureChanged(new TreePathEventArgs(parent));
        }

        private int GetNextID()
        {
            int max = 1;
            for (int i = 0; i < m_table.Rows.Count; i++)
            {
                int id = Convert.ToInt32(m_table.Rows[i]["ID"]);
                if (id > max)
                    max = id;
            }

            return max + 1;
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.