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

1 comment: