Solved

TreeView Control

Posted on 2008-06-21
12
737 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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
 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

617 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