Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

TreeView Control

Posted on 2008-06-21
12
Medium Priority
?
738 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

704 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