Sunday, December 8, 2013

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);
            }
        }
    }          
}



No comments:

Post a Comment