Sunday, December 8, 2013

Load treeview from datasource with hardcoded root nodes on treeview (Windows Forms C#)



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Data.SqlClient;


namespace ReportBuilder
{
    public partial class MainForm : Form
    {
        TreeNode parent = null;
        TreeNode child = null;
        TreeNode grandchild = null;      
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            string groupmasterparent = "select GroupID,GroupDesc as ParentGroupDesc,ParentGroupID from aGroupMaster where AccountType='A' and ParentGroupID is null";
            SqlCommand groupmasterparentcmd = new SqlCommand(groupmasterparent, DBConnection.Getconnection());
            groupmasterparentcmd.CommandType = CommandType.Text;
            SqlDataAdapter groupmasterparentda = new SqlDataAdapter(groupmasterparentcmd);        
            DataSet ds = new DataSet();          
            groupmasterparentda.Fill(ds, "groupmasterparentdetails");          
            foreach (DataRow dr in ds.Tables["groupmasterparentdetails"].Rows)
            {              
                parent = treeView1.Nodes[0].Nodes.Add(dr["ParentGroupDesc"].ToString());
                PopulateChildinTreeView(Convert.ToInt32(dr["GroupID"].ToString()), parent);
            }          
        }
        private void PopulateChildinTreeView(int parentID, TreeNode parent)
        {                                
            string groupmaster = "select GroupID,GroupDesc,ParentGroupID from aGroupMaster where ParentGroupID="+parentID+"";          
            SqlCommand groupmastercmd = new SqlCommand(groupmaster, DBConnection.Getconnection());          
            groupmastercmd.CommandType = CommandType.Text;          
            SqlDataAdapter groupmasterda = new SqlDataAdapter(groupmastercmd);          
            DataSet ds = new DataSet();
            groupmasterda.Fill(ds, "groupmasterdetails");  
            if (ds.Tables["groupmasterdetails"].Rows.Count > 0)
            {
                foreach (DataRow drChild in ds.Tables["groupmasterdetails"].Rows)
                {
                    if (parent == null)
                        child = treeView1.Nodes[0].Nodes.Add(drChild["GroupDesc"].ToString());
                    else
                        child = parent.Nodes.Add(drChild["GroupDesc"].ToString());
                    PopulateChildinTreeView(Convert.ToInt32(drChild["GroupID"].ToString()), child);
                }              
            }
            else
                PopulateGrandChildinTreeView(parentID, parent);
        }

        private void PopulateGrandChildinTreeView(int childID, TreeNode child)
        {                      
            string accountmaster = "select * from aAccountMaster where groupid="+childID+"";          
            SqlCommand accountmastercmd = new SqlCommand(accountmaster, DBConnection.Getconnection());          
            accountmastercmd.CommandType = CommandType.Text;          
            SqlDataAdapter accountmasterda = new SqlDataAdapter(accountmastercmd);
            DataSet ds = new DataSet();          
            accountmasterda.Fill(ds, "accountmasterdetails");
            foreach (DataRow drChild in ds.Tables["accountmasterdetails"].Rows)
            {
                if (child == null)
                    grandchild = parent.Nodes.Add(drChild["AccountDesc"].ToString());
                else
                    grandchild = child.Nodes.Add(drChild["AccountDesc"].ToString());
            }
        }
    }
}

Load DeveExpress treelistview from datasource with hardcoded root nodes on treelistview (Windows Forms in C#)



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Data.SqlClient;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraTreeList.Columns;


namespace ReportBuilder
{
    public partial class MainForm : Form
    {      
        TreeListNode rootnodeAsset = null;
        TreeListNode rootnodeLiability = null;
        TreeListNode rootnodeIncome = null;
        TreeListNode rootnodeExpense= null;
        TreeListNode ParentNode = null;
        TreeListNode ChildNode = null;
        TreeListNode GrandChildNode = null;
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            string groupmasterparent = "select GroupID,GroupDesc as ParentGroupDesc,ParentGroupID from aGroupMaster where AccountType='A' and ParentGroupID is null";
            SqlCommand groupmasterparentcmd = new SqlCommand(groupmasterparent, DBConnection.Getconnection());
            groupmasterparentcmd.CommandType = CommandType.Text;
            SqlDataAdapter groupmasterparentda = new SqlDataAdapter(groupmasterparentcmd);        
            DataSet ds = new DataSet();          
            groupmasterparentda.Fill(ds, "groupmasterparentdetails");
            rootnodeAsset = trlstGroupMaster.AppendNode(new object[] { "Asset" }, null);
            rootnodeLiability = trlstGroupMaster.AppendNode(new object[] { "Liability" }, null);
            rootnodeIncome = trlstGroupMaster.AppendNode(new object[] { "Income" }, null);
            rootnodeExpense = trlstGroupMaster.AppendNode(new object[] { "Expense" }, null);
            foreach (DataRow dr in ds.Tables["groupmasterparentdetails"].Rows)
            {
                ParentNode = trlstGroupMaster.AppendNode(new object[] { dr["ParentGroupDesc"].ToString() }, rootnodeAsset);
                dxPopulateChildinTreeView(Convert.ToInt32(dr["GroupID"].ToString()), ParentNode);              
            }
                       
        }
        private void dxPopulateChildinTreeView(int parentID, TreeListNode ParentNode)
        {
            string groupmaster = "select GroupID,GroupDesc,ParentGroupID from aGroupMaster where ParentGroupID=" + parentID + "";
            SqlCommand groupmastercmd = new SqlCommand(groupmaster, DBConnection.Getconnection());
            groupmastercmd.CommandType = CommandType.Text;
            SqlDataAdapter groupmasterda = new SqlDataAdapter(groupmastercmd);
            DataSet ds = new DataSet();
            groupmasterda.Fill(ds, "groupmasterdetails");  
            if (ds.Tables["groupmasterdetails"].Rows.Count > 0)
            {
                foreach (DataRow drChild in ds.Tables["groupmasterdetails"].Rows)
                {
                    if (ParentNode == null)
                        ChildNode = trlstGroupMaster.AppendNode(new object[] { drChild["GroupDesc"].ToString() }, rootnodeAsset);
                    else
                        ChildNode = trlstGroupMaster.AppendNode(new object[] { drChild["GroupDesc"].ToString() }, ParentNode);
                    dxPopulateChildinTreeView(Convert.ToInt32(drChild["GroupID"].ToString()), ChildNode);
                }
            }
            else
                dxPopulateGrandChildinTreeView(parentID, ParentNode);
        }

        private void dxPopulateGrandChildinTreeView(int childID, TreeListNode ChildNode)
        {
            string accountmaster = "select * from aAccountMaster where groupid=" + childID + "";
            SqlCommand accountmastercmd = new SqlCommand(accountmaster, DBConnection.Getconnection());
            accountmastercmd.CommandType = CommandType.Text;
            SqlDataAdapter accountmasterda = new SqlDataAdapter(accountmastercmd);
            DataSet ds = new DataSet();
            accountmasterda.Fill(ds, "accountmasterdetails");
            foreach (DataRow drGrandChild in ds.Tables["accountmasterdetails"].Rows)
            {
                if (ChildNode == null)
                    GrandChildNode = trlstGroupMaster.AppendNode(new object[] { drGrandChild["AccountDesc"].ToString() }, ParentNode);
                else
                    GrandChildNode = trlstGroupMaster.AppendNode(new object[] { drGrandChild["AccountDesc"].ToString() }, ChildNode);
            }
        }
    }          
}