Populate N-Level TreeView from multiple tables using C# in Windows Application

Last Reply 8 months ago By Jesus

Posted 8 months ago

How to Populate treeview from 2 SQL tables with n Levels

on this link is only one table, I need same thing from 3 tables.

Populate Multi-Level (N-Level) TreeView using C# and VB.Net in Windows Application

this is the code I use to load data to the treeview, this is working well enougth, but is only for 2 layer.

        private void llenarTreeView()
        {
            DataTable dt = new DataTable();
            dt = CADArea.GetData();
            foreach (DataRow dr in dt.Rows)
            {
                parentNode = treeView1.Nodes.Add(dr["Area"].ToString());
                PopulateTreeView(Guid.Parse(dr["IdArea"].ToString()), parentNode);
            }
            treeView1.ExpandAll();
        }
 
        private void PopulateTreeView(Guid IdArea, TreeNode parentNode)
        {
            DataTable dtble = new DataTable();
            dtble = CADMaquinas.SelectMaquinasbyIdArea(IdArea);
            TreeNode childNode;
            foreach (DataRow dr in dtble.Rows)
            {
                if (parentNode == null)
                    childNode = treeView1.Nodes.Add(dr["Maquina"].ToString());
                else
                    childNode = parentNode.Nodes.Add(dr["Maquina"].ToString());
                PopulateTreeView(Guid.Parse(dr["IdMaquina"].ToString()), childNode);
            }
        }

 

Posted 8 months ago

Hello Sir. after several days trying to bulid this thing by my self, finally, succes.

What i did was modify my table set up and the way I load the data to tableAdapters and Datatables. 

USE [Mantto2]
GO

/****** Object:  Table [dbo].[Areas]    Script Date: 09/08/2020 11:58:24 a. m. ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Areas](
	[IdArea] [uniqueidentifier] NOT NULL,
	[Area] [nvarchar](50) NOT NULL,
	[esActivo] [bit] NOT NULL,
 CONSTRAINT [PK_Areas] PRIMARY KEY CLUSTERED 
(
	[IdArea] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Areas] ADD  CONSTRAINT [DF_Areas_IdArea]  DEFAULT (newid()) FOR [IdArea]
GO


and the other Table


USE [Mantto2]
GO

/****** Object:  Table [dbo].[Maquinas]    Script Date: 09/08/2020 11:59:52 a. m. ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Maquinas](
	[IdMaquina] [uniqueidentifier] NOT NULL,
	[IdArea] [uniqueidentifier] NULL,
	[IdPadre] [uniqueidentifier] NOT NULL,
	[Maquina] [nvarchar](50) NOT NULL,
	[esActivo] [bit] NOT NULL,
	[IdDatosMaquina] [uniqueidentifier] NULL,
 CONSTRAINT [PK_Maquinas] PRIMARY KEY CLUSTERED 
(
	[IdMaquina] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Maquinas] ADD  CONSTRAINT [DF_Maquinas_IdMaquina]  DEFAULT (newid()) FOR [IdMaquina]
GO

ALTER TABLE [dbo].[Maquinas] ADD  CONSTRAINT [DF__Maquinas__IdMaqu__01142BA1]  DEFAULT (NULL) FOR [IdPadre]
GO

ALTER TABLE [dbo].[Maquinas]  WITH CHECK ADD  CONSTRAINT [FK_Maquinas_Areas] FOREIGN KEY([IdArea])
REFERENCES [dbo].[Areas] ([IdArea])
GO

ALTER TABLE [dbo].[Maquinas] CHECK CONSTRAINT [FK_Maquinas_Areas]
GO

and the modify the code in VS as follows

        private void frmMaquinas_Load(object sender, EventArgs e)
        {
            // TODO: esta línea de código carga datos en la tabla 'mantto2DataSet.Areas' Puede moverla o quitarla según sea necesario.
            this.areasTableAdapter.Fill(this.mantto2DataSet.Areas);
            // TODO: esta línea de código carga datos en la tabla 'mantto2DataSet.Maquinas' Puede moverla o quitarla según sea necesario.
            this.maquinasTableAdapter.Fill(this.mantto2DataSet.Maquinas);

            llenarTreeView();
        }

        private void llenarTreeView()
        {
            DataTable dt = new DataTable();
            dt = CADArea.SelectAllbyArea();
            foreach (DataRow dr in dt.Rows)
            {
                parentNode = treeView1.Nodes.Add(dr["Area"].ToString());
                PopulateTreeView(Guid.Parse(dr["IdArea"].ToString()), parentNode);
            }
            treeView1.ExpandAll();
        }

        private void PopulateTreeView(Guid IdArea, TreeNode parentNode)
        {
            DataTable dtble = new DataTable();
            Guid IdPadre = IdArea;
            dtble = CADMaquinas.SelectAllbyIdPadreOrderbyMaquina(IdPadre);
            TreeNode childNode;
            if (dtble.Rows.Count != 0)
            foreach (DataRow dr in dtble.Rows)
            {
                if (parentNode == null)
                    childNode = treeView1.Nodes.Add(dr["Maquina"].ToString());
                else
                    childNode = parentNode.Nodes.Add(dr["Maquina"].ToString());
                PopulateTreeView(Guid.Parse(dr["IdMaquina"].ToString()), childNode);
            }
            else
            {
                DataTable dtmaq = new DataTable();
                dtmaq = CADMaquinas.SelectAllbyIdPadreOrderbyMaquina(IdPadre);
                TreeNode granChildNode;
                foreach (DataRow drm in dtmaq.Rows)
                {
                    if (parentNode == null)
                    {
                        granChildNode = treeView1.Nodes.Add(drm["Maquina"].ToString());
                    }
                    else
                        granChildNode = treeView1.Nodes.Add(drm["Maquina"].ToString());
                    PopulateTreeView(Guid.Parse(drm["IdMaquina_padre"].ToString()), granChildNode);
                }

Thakyou for all.