Solved

TreeView Control

Posted on 2008-06-21
12
735 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expression Evaluater 3 46
need to convert function to c# 5 45
How to repeat the data 4 30
VB.NET - Refactor Class per SOLID principles 2 33
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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