Solved

TreeView Control

Posted on 2008-06-21
12
731 Views
Last Modified: 2011-10-19
Attached to this question is a text file to create some tables and insert data into those tables. What i am trying to do is create a TreeView dervived from the information contained in those tables. So, as you see, there is a table for server status, server, and database. What I want the tree to look like is:

- Server Status
  - Server
    - Database

Can anyone provide me with the C# code that does this?

PS - The attached sql script is missing a database creation.
sql.txt
0
Comment
Question by:computerstreber
  • 6
  • 5
12 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21839911
I see you have asked this many times. Looks like you need to hire a programmer.
I suggest you to focus on some specific question.
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 21840199
You can achieve this easily with ADO.NET to be honest.  As you can create relationships between tables and iterate through child rows.  Create a binding list and maybe a dependancy somewhere and you are l;aughing.  One min I will drum up an example!

Andrew :-)
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 21840234
And here it is.  Just add a treeview to the form, and here is the code for such a form.

Cheers,

Andrew :-)
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;
 

namespace WindowsForm_Examples_NET_2

{

    public partial class ServerTree : Form

    {

        private DataSet mainDS;

        private DataTable statusTable;

        private DataTable serverTable;

        private DataTable databaseTable;
 

        public ServerTree()

        {

            InitializeComponent();

        }
 

        private void ServerTree_Load(object sender, EventArgs e)

        {

            mainDS =  new DataSet();
 
 

            using(SqlConnection sc1 = new SqlConnection("Data Source=REA_ANDREW-PC\\SQLEXPRESS;Initial Catalog=PTracker;Integrated Security=True"))

            {

                SqlCommand multipleCommand = sc1.CreateCommand();

                multipleCommand.CommandText = "select * from ServerStatus;select * from NetworkServer;select * from SQLServer;";

                multipleCommand.CommandType = CommandType.Text;

                SqlDataAdapter sda1 = new SqlDataAdapter(multipleCommand);

                sda1.Fill(mainDS);
 

                statusTable = mainDS.Tables[0];

                serverTable = mainDS.Tables[1];

                databaseTable = mainDS.Tables[2];
 

                DataRelation statusVsServer = new DataRelation("statusVsServer",statusTable.Columns["StatusID"],serverTable.Columns["ServerStatusID"]);

                DataRelation serverVsDatabase = new DataRelation("serverVsDatabase", serverTable.Columns["ServerID"], databaseTable.Columns["ServerID"]);
 

                mainDS.Relations.Add(statusVsServer);

                mainDS.Relations.Add(serverVsDatabase);
 

                foreach(DataRow dr in statusTable.Rows)

                {

                    TreeNode t = new TreeNode(dr["Status"].ToString());

                    treeView1.Nodes.Add(t);

                    foreach(DataRow childRow in dr.GetChildRows(statusVsServer))

                    {

                        TreeNode s = new TreeNode(childRow["ServerName"].ToString());

                        t.Nodes.Add(s);

                        foreach(DataRow childchildRow in childRow.GetChildRows(serverVsDatabase))

                        {

                            TreeNode r = new TreeNode(childchildRow["SQLServerName"].ToString());

                            s.Nodes.Add(r);

                        }

                    }

                }
 

            }

        }

    }

}

Open in new window

0
 
LVL 3

Author Comment

by:computerstreber
ID: 21841530


I tried to implent the script you provided and I am getting this error message when I try to compile:

'SQLTracker.Form1' does not contain a definition for 'treeView1_AfterSelect'
source.txt
0
 
LVL 3

Author Comment

by:computerstreber
ID: 21841533
Sorry about the previous post... Here is the code...


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;
 

namespace SQLTracker

{

    public partial class Form1 : Form

    {

        private DataSet mainDS;

        private DataTable statusTable;

        private DataTable serverTable;

        private DataTable databaseTable;
 

        public Form1()

        {

            InitializeComponent();

        }
 

        private void treeView1_Load(object sender, TreeViewEventArgs e)

        {

            mainDS = new DataSet();

            using (SqlConnection sc1 = new SqlConnection("Data Source=themarxfamily;Initial Catalog=SQLTracker;Integrated Security=True"))

            {

                SqlCommand multipleCommand = sc1.CreateCommand();

                multipleCommand.CommandText = "SELECT * FROM ServerStatus; SELECT * FROM NetworkServer; SELECT * FROM SQLServer;";

                multipleCommand.CommandType = CommandType.Text;

                SqlDataAdapter sda1 = new SqlDataAdapter(multipleCommand);

                sda1.Fill(mainDS);
 

                statusTable = mainDS.Tables[0];

                serverTable = mainDS.Tables[1];

                databaseTable = mainDS.Tables[2];
 

                DataRelation statusVsServer = new DataRelation("statusVsServer", statusTable.Columns["ServerStatusID"], serverTable.Columns["ServerStatusID"]);

                DataRelation serverVsDatabase = new DataRelation("serverVsDatabase", serverTable.Columns["ServerID"], databaseTable.Columns["ServerID"]);
 

                mainDS.Relations.Add(statusVsServer);

                mainDS.Relations.Add(serverVsDatabase);
 

                foreach (DataRow dr in statusTable.Rows)

                {

                    TreeNode t = new TreeNode(dr["Status"].ToString());

                    treeView1.Nodes.Add(t);

                    foreach (DataRow childRow in dr.GetChildRows(statusVsServer))

                    {

                        TreeNode s = new TreeNode(childRow["ServerName"].ToString());

                        t.Nodes.Add(s);

                        foreach (DataRow childchildRow in childRow.GetChildRows(serverVsDatabase))

                        {

                            TreeNode r = new TreeNode(childchildRow["SQLServerName"].ToString());

                            s.Nodes.Add(r);

                        }

                    }

                }

            }

        }

    }

}

Open in new window

0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 21841849
I have implemented that event on the actual treeview in my example.  What diod you want to achieve using it.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 3

Author Comment

by:computerstreber
ID: 21842632
I am just trying to create that tree. I implemeneted the code as you have above, and finally got past the error message, but nothing gets populated in the application....
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;
 

namespace SQLTracker

{

    public partial class Form1 : Form

    {

        private DataSet mainDS;

        private DataTable statusTable;

        private DataTable serverTable;

        private DataTable databaseTable;
 

        public Form1()

        {

            InitializeComponent();

        }
 

        private void treeView1_Load(object sender, EventArgs e)

        {

            mainDS = new DataSet();
 

            using (SqlConnection sqlConn = new SqlConnection("Data Source=themarxfamily;Initial Catalog=SQLTracker;Integrated Security=True"))

            {

                SqlCommand multipleCommand = sqlConn.CreateCommand();

                multipleCommand.CommandText = "select * from dbo.ServerStatus;select * from dbo.NetworkServer;select * from dbo.SQLServer;";

                multipleCommand.CommandType = CommandType.Text;

                SqlDataAdapter sqlDataAdapt = new SqlDataAdapter(multipleCommand);

                sqlDataAdapt.Fill(mainDS);
 

                statusTable = mainDS.Tables[0];

                serverTable = mainDS.Tables[1];

                databaseTable = mainDS.Tables[2];
 

                DataRelation statusVsServer = new DataRelation("statusVsServer",statusTable.Columns["ServerStatusID"], serverTable.Columns["ServerStatusID"]);

                DataRelation serverVsDatabase = new DataRelation("serverVsDatabase", serverTable.Columns["ServerID"], databaseTable.Columns["ServerID"]);
 

                mainDS.Relations.Add(statusVsServer);

                mainDS.Relations.Add(serverVsDatabase);
 

                foreach (DataRow dr in statusTable.Rows)

                {

                    TreeNode t = new TreeNode(dr["StatusName"].ToString());

                    treeView1.Nodes.Add(t);

                    foreach (DataRow childRow in dr.GetChildRows(statusVsServer))

                    {

                        TreeNode s = new TreeNode(childRow["ServerName"].ToString());

                        t.Nodes.Add(s);

                        foreach (DataRow childchildRow in childRow.GetChildRows(serverVsDatabase))

                        {

                            TreeNode r = new TreeNode(childchildRow["SQLServerName"].ToString());

                            s.Nodes.Add(r);

                        }

                    }

                }

            }

        }
 

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)

        {
 

        }

    }

}

Open in new window

screen.bmp
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 21844058
the tables in your database tables.  You have populated them with that test info you gave us haven't you.  I will post a screen shot of what mine looks like
0
 
LVL 20

Accepted Solution

by:
REA_ANDREW earned 500 total points
ID: 21844066
Here is my screen shot
21844058.gif
0
 
LVL 3

Author Comment

by:computerstreber
ID: 21850152
Here are the steps I took...

1. Create Project
2. Connect to Database
step1-CreateProject.bmp
step2-connectDB.bmp
0
 
LVL 3

Author Comment

by:computerstreber
ID: 21850160
3. Create Database
USE [master]

GO
 

CREATE DATABASE [dbo].[SQLTracker];

GO
 

CREATE TABLE [dbo].[Status] (

	  StatusID		INT		IDENTITY(1,1)	PRIMARY KEY

	, StatusDesc	VARCHAR(25)	NOT NULL

);

GO
 

-- Test Data

INSERT INTO [dbo].[Status] VALUES ('Online');

INSERT INTO [dbo].[Status] VALUES ('Offline');

INSERT INTO [dbo].[Status] VALUES ('Shutdown');

INSERT INTO [dbo].[Status] VALUES ('Unkown');

GO
 

CREATE TABLE [dbo].[Servers] (

	  ServerID		INT		IDENTITY(1,1)	PRIMARY KEY

	, ServerName	VARCHAR(25)	NOT NULL

	, StatusID		INT		REFERENCES [dbo].[Status]([StatusID])

);

GO
 

-- Test Data

INSERT INTO [dbo].[Servers] VALUES ('1230',1);

INSERT INTO [dbo].[Servers] VALUES ('1231',2);

INSERT INTO [dbo].[Servers] VALUES ('1232',3);

INSERT INTO [dbo].[Servers] VALUES ('1233',4);

INSERT INTO [dbo].[Servers] VALUES ('1234',1);

INSERT INTO [dbo].[Servers] VALUES ('1235',2);

INSERT INTO [dbo].[Servers] VALUES ('1236',3);

INSERT INTO [dbo].[Servers] VALUES ('1237',4);

INSERT INTO [dbo].[Servers] VALUES ('1238',5);

GO
 

CREATE TABLE [dbo].[SQLDatabases] (

	  SQLDatabaseID		INT		IDENTITY(1,1)	PRIMARY KEY

	, SQLDatabaseName	VARCHAR(25)	NOT NULL

	, ServerID			INT		REFERENCES [dbo].[Server]([ServerID])

)

Open in new window

0
 
LVL 3

Author Comment

by:computerstreber
ID: 21850232
4. Drag and Drop a TreeView control to the form.
5. Put in code as describe in your prior post.

However, it doesn't appear to work...

Is there something else I need to change with the database connection?

CORRECTION TO SQL ABOVE:
USE [master]

GO
 

CREATE DATABASE [SQLTracker];

GO
 

USE [SQLTracker]

GO
 

CREATE TABLE [dbo].[Status] (

	  StatusID		INT		IDENTITY(1,1)	PRIMARY KEY

	, StatusDesc	VARCHAR(25)	NOT NULL

);

GO
 

-- Test Data

INSERT INTO [dbo].[Status] VALUES ('Online');

INSERT INTO [dbo].[Status] VALUES ('Offline');

INSERT INTO [dbo].[Status] VALUES ('Shutdown');

INSERT INTO [dbo].[Status] VALUES ('Unkown');

GO
 

CREATE TABLE [dbo].[Servers] (

	  ServerID		INT		IDENTITY(1,1)	PRIMARY KEY

	, ServerName	VARCHAR(25)	NOT NULL

	, StatusID		INT		REFERENCES [dbo].[Status]([StatusID])

);

GO
 

-- Test Data

INSERT INTO [dbo].[Servers] VALUES ('1230',1);

INSERT INTO [dbo].[Servers] VALUES ('1231',2);

INSERT INTO [dbo].[Servers] VALUES ('1232',3);

INSERT INTO [dbo].[Servers] VALUES ('1233',4);

INSERT INTO [dbo].[Servers] VALUES ('1234',1);

INSERT INTO [dbo].[Servers] VALUES ('1235',2);

INSERT INTO [dbo].[Servers] VALUES ('1236',3);

INSERT INTO [dbo].[Servers] VALUES ('1237',4);

GO
 

CREATE TABLE [dbo].[SQLDatabases] (

	  SQLDatabaseID		INT		IDENTITY(1,1)	PRIMARY KEY

	, SQLDatabaseName	VARCHAR(25)	NOT NULL

	, ServerID			INT		REFERENCES [dbo].[Servers]([ServerID])

)

GO
 

INSERT INTO [dbo].[SQLDatabases] VALUES ('DB1',1);

INSERT INTO [dbo].[SQLDatabases] VALUES ('DB2',1);

INSERT INTO [dbo].[SQLDatabases] VALUES ('DB3',2);

INSERT INTO [dbo].[SQLDatabases] VALUES ('DB4',2);

INSERT INTO [dbo].[SQLDatabases] VALUES ('DB5',3);

INSERT INTO [dbo].[SQLDatabases] VALUES ('DB6',3);

INSERT INTO [dbo].[SQLDatabases] VALUES ('DB7',4);

INSERT INTO [dbo].[SQLDatabases] VALUES ('DB8',4);

INSERT INTO [dbo].[SQLDatabases] VALUES ('DB9',5);

INSERT INTO [dbo].[SQLDatabases] VALUES ('DB10',5);

INSERT INTO [dbo].[SQLDatabases] VALUES ('DB11',6);

INSERT INTO [dbo].[SQLDatabases] VALUES ('DB12',6);

INSERT INTO [dbo].[SQLDatabases] VALUES ('DB13',7);

INSERT INTO [dbo].[SQLDatabases] VALUES ('DB14',7);

INSERT INTO [dbo].[SQLDatabases] VALUES ('DB15',8);

INSERT INTO [dbo].[SQLDatabases] VALUES ('DB16',8);

GO

Open in new window

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now