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
Solved

TreeView Control

Posted on 2008-06-21
12
734 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Printing 1 58
Create a WebAPI using a generic return parameter 9 30
RegEx with optional part 4 42
linq, c# 8 22
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 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