Treeview master detail

rebotea

Member
Joined
Sep 20, 2016
Messages
16
Programming Experience
Beginner
Hi

I have 4 tables in Mysql how can i make a DataRelation in my database and display Data like the image below in my windows Aplicacion.

Thank?s

C#:
Select * from a1_locaismaq
Select * from a2_maquinas
Select * from a3_ponto_lub


Image 14.jpg
imag.JPG
 
Here's an example that will populate three levels in a WinForms TreeView from three related database tables. You can extend the principle to as many layers as you like.
private readonly DataSet data = new DataSet();

private DataTable parentTable;
private DataTable childTable;
private DataTable grandChildTable;

private DataRelation parentChildRelation;
private DataRelation childGrandChildRelation;

private void Form1_Load(object sender, EventArgs e)
{
    var connection = new SqlConnection("connection string here");

    var parentAdapter = new SqlDataAdapter("SELECT ParentId, ParentName FROM Parent", connection);
    var childAdapter = new SqlDataAdapter("SELECT ParentId, ChildId, ChildName FROM Child", connection);
    var grandChildAdapter = new SqlDataAdapter("SELECT ChildId, GrandChildId, GrandChildName FROM GrandChild", connection);

    connection.Open();
    parentAdapter.Fill(data, "Parent");
    childAdapter.Fill(data, "Child");
    grandChildAdapter.Fill(data, "GrandChild");
    connection.Close();

    parentTable = data.Tables["Parent"];
    childTable = data.Tables["Child"];
    grandChildTable = data.Tables["GrandChild"];

    parentChildRelation = data.Relations.Add(parentTable.Columns["ParentId"], childTable.Columns["ChildId"]);
    childGrandChildRelation = data.Relations.Add(childTable.Columns["ChildId"], grandChildTable.Columns["GrandChild"]);

    this.treeView1.Nodes.AddRange(GetParentNodes());
}

private TreeNode[] GetParentNodes()
{
    var parentNodes = new List<TreeNode>();

    foreach (var parentRow in parentTable.AsEnumerable())
    {
        var parentNode = new TreeNode((string) parentRow["ParentName"]);

        parentNode.Nodes.AddRange(GetChildNodes(parentRow));
        parentNodes.Add(parentNode);
    }

    return parentNodes.ToArray();
}

private TreeNode[] GetChildNodes(DataRow parentRow)
{
    var childNodes = new List<TreeNode>();

    foreach (var childRow in parentRow.GetChildRows(parentChildRelation))
    {
        var childNode = new TreeNode((string) childRow["ChildName"]);

        childNode.Nodes.AddRange(GetGrandChildNodes(childRow));
        childNodes.Add(childNode);
    }

    return childNodes.ToArray();
}

private TreeNode[] GetGrandChildNodes(DataRow childRow)
{
    var grandChildNodes = new List<TreeNode>();

    foreach (var grandChildRow in childRow.GetChildRows(childGrandChildRelation))
    {
        var grandChildNode = new TreeNode((string) grandChildRow["GrandChildName"]);

        grandChildNodes.Add(grandChildNode);
    }

    return grandChildNodes.ToArray();
}

Note that I've used SqlClient types but it would be exactly the same with MySqlClient types.
 
THank you is thi for asp.net webform ? i get error Here i dont have "AddRange"
this.treeView1.Nodes.AddRange(GetParentNodes());
childNode.Nodes.AddRange(GetGrandChildNodes(childRow));
Thank?s


C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data;
using MySql.Data.MySqlClient;
 
using System.Data;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
private readonly DataSet data = new DataSet();

    private DataTable parentTable;
    private DataTable childTable;
    private DataTable grandChildTable;

    private DataRelation parentChildRelation;
    private DataRelation childGrandChildRelation;

    private void Form1_Load(object sender, EventArgs e)
    {
        var connection = new MySqlConnection("connection string here");

        var parentAdapter = new MySqlDataAdapter("SELECT ParentId, ParentName FROM Parent", connection);
        var childAdapter = new MySqlDataAdapter("SELECT ParentId, ChildId, ChildName FROM Child", connection);
        var grandChildAdapter = new MySqlDataAdapter("SELECT ChildId, GrandChildId, GrandChildName FROM GrandChild", connection);

        connection.Open();
        parentAdapter.Fill(data, "Parent");
        childAdapter.Fill(data, "Child");
        grandChildAdapter.Fill(data, "GrandChild");
        connection.Close();

        parentTable = data.Tables["Parent"];
        childTable = data.Tables["Child"];
        grandChildTable = data.Tables["GrandChild"];

        parentChildRelation = data.Relations.Add(parentTable.Columns["ParentId"], childTable.Columns["ChildId"]);
        childGrandChildRelation = data.Relations.Add(childTable.Columns["ChildId"], grandChildTable.Columns["GrandChild"]);

        this.TreeView1.Nodes.AddAt(GetParentNodes());
    }

    private TreeNode[] GetParentNodes()
    {
        var parentNodes = new List<TreeNode>();

        foreach (var parentRow in parentTable.AsEnumerable())
        {
            var parentNode = new TreeNode((string)parentRow["ParentName"]);

            parentNode.Nodes.AddRange(GetChildNodes(parentRow));
            parentNodes.Add(parentNode);
        }

        return parentNodes.ToArray();
    }

    private TreeNode[] GetChildNodes(DataRow parentRow)
    {
        var childNodes = new List<TreeNode>();

        foreach (var childRow in parentRow.GetChildRows(parentChildRelation))
        {
            var childNode = new TreeNode((string)childRow["ChildName"]);

            childNode.Nodes.AddRange(GetGrandChildNodes(childRow));
            childNodes.Add(childNode);
        }

        return childNodes.ToArray();
    }

    private TreeNode[] GetGrandChildNodes(DataRow childRow)
    {
        var grandChildNodes = new List<TreeNode>();

        foreach (var grandChildRow in childRow.GetChildRows(childGrandChildRelation))
        {
            var grandChildNode = new TreeNode((string)grandChildRow["GrandChildName"]);

            grandChildNodes.Add(grandChildNode);
        }

        return grandChildNodes.ToArray();
    }

}
 
Last edited:
Sorry, I missed what forum this question was posted in. The principle is still the same though. You'll just have to look at how you usually add nodes and child nodes in a Web Forms TreeView and adjust accordingly.
 
Thank`s for your time
I?m tryng to do a Multiuser WebAplicacion when i get my selected Node can i open Page1 if it is a master node or page2 is it is a chield Node??

protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
this.TextBox1.Text = TreeView1.SelectedNode.Value.ToString();
this.TextBox2.Text = "You selected: " + TreeView1.SelectedNode.Text;

}
 
Thank?s can i make a join and make a Treeview from a single query?
FROM
SELECT
`a1_locaismaq`.`clienteninc`,
`a1_locaismaq`.`localmaq_inc`,
`a2_maquinas`.`maquinas_ninc`,
`a3_ponto_lub`.`maquinas_ninc`,
`a2_maquinas`.`localmaq_inc`,
.................................................
`a2_maquinas`
INNER JOIN `a3_ponto_lub` ON (`a2_maquinas`.`maquinas_ninc` = `a3_ponto_lub`.`maquinas_ninc`)
INNER JOIN `a1_locaismaq` ON (`a1_locaismaq`.`localmaq_inc` = `a2_maquinas`.`localmaq_inc`)
INNER JOIN `a4_materias` ON (`a3_ponto_lub`.`ponto_inc` = `a4_materias`.`ponto_inc`)
INNER JOIN `clientes` ON (`a1_locaismaq`.`clienteninc` = `clientes`.`clienteninc`)
 
Firstly, please use QUOTE tags only for quotes. Not surprisingly, CODE tags are for code.
Thank?s can i make a join and make a Treeview from a single query?
Absolutely, e.g.
var sql = @"SELECT p.ParentId, ParentName, ChildId, ChildName
            FROM Parent p INNER JOIN Child c ON p.ParentId = c.ParentId
            ORDER BY p.ParentId, ChildId";
var adapter = new SqlDataAdapter(sql, connectionString);
var table = new DataTable();

adapter.Fill(table);

int currentParentId = 0;

foreach (DataRow row in table.Rows)
{
    var parentId = (int) row["ParentId"];

    if (parentId != currentParentId)
    {
        // New parent.
        // ...

        currentParentId = parentId;
    }

    // ...
}
 
Query is done as for the tree can you give some help please

string ConnString = ConfigurationManager.ConnectionStrings["DBMANUTEN"].ConnectionString;


MySqlConnection conn = new MySqlConnection(ConnString);

var sql = @"SELECT clientes.clienteninc,clientes.morada,clientes.clientenome, a1_locaismaq.local_obs, a1_locaismaq.local, a1_locaismaq.clienteninc,a1_locaismaq.localmaq_inc,a2_maquinas.maquinas_ninc,a2_maquinas.localmaq_inc, a2_maquinas.maq_nome,a3_ponto_lub.maquinas_ninc, a3_ponto_lub.ponto_inc, a3_ponto_lub.tag_codigo, a3_ponto_lub.serv_codig,a3_ponto_lub.ponto_designa,a3_ponto_lub.local,a3_ponto_lub.maq_nome,a3_ponto_lub.maq_ativa, a3_ponto_lub.maq_local FROM clientes
INNER JOIN a1_locaismaq ON(clientes.clienteninc = a1_locaismaq.clienteninc)
INNER JOIN a2_maquinas ON(a1_locaismaq.localmaq_inc = a2_maquinas.localmaq_inc)
INNER JOIN a3_ponto_lub ON(a2_maquinas.maquinas_ninc = a3_ponto_lub.maquinas_ninc)";

var adapter = new MySqlDataAdapter(sql, ConnString);
var TrevCliente = new DataTable();

adapter.Fill(TrevCliente);
 
Thank?s
and for the Chield of table a1_locaismaq and a2_maquinas and a3_ponto_lub.

C#:
          foreach (DataRow row in TrevCliente.Rows)
            {
                var parentId = (int)row["clienteninc"];
            
                if (parentId != currentParentId)
                {
                    TreeNode node = new TreeNode();
                node.Text = row["morada"].ToString();
                node.Value = row["clienteninc"].ToString();

                    // New parent.
                    // ...

                    currentParentId = parentId;
                   TreeView1.Nodes.Add(node);
                }

                foreach (DataRow childRow in parentId.GetChildRows("SecondHierarchy"))
                {
                    TreeNode childNode = new TreeNode((string)childRow["ChildText"], Convert.ToString(childRow["ChildID"]));
                    masterNode.ChildNodes.Add(childNode);
                    childNode.Value = Convert.ToString(childRow["Child"]);
                }
                // ...
            }
 
Last edited by a moderator:
I asked you earlier not to use QUOTE tags for code and you did it again. I've fixed it for you this time but next time I'll simply ignore the post. If posting your code properly is too much trouble for you then reading it is too much trouble for me.

With three levels, my previous example becomes this:
var sql = @"SELECT p.ParentId, ParentName, ChildId, ChildName
            FROM Parent p INNER JOIN Child c ON p.ParentId = c.ParentId
            ORDER BY p.ParentId, ChildId";
var adapter = new SqlDataAdapter(sql, connectionString);
var table = new DataTable();

adapter.Fill(table);

int currentParentId = 0;
int currentChildId = 0;

foreach (DataRow row in table.Rows)
{
    var parentId = (int) row["ParentId"];
    var childId = (int) row["ChildId"];

    if (childId != currentChildId)
    {
        // New child.
        // ...

        if (parentId != currentParentId)
        {
            // New parent.
            // ...

            currentParentId = parentId;
        }

        // ...

        currentChildId = childId;
    }

    // ...
 
Hi jmcilhinney

Wow can i go to other page if i click in the chield of the TreeView (../../..).
Thank You

Of course you can, but that really has nothing to do with the topic of this thread so I won't be answering it in this thread. If you have a question on a new topic then you should start a new thread with a title that describes that topic.
 

Latest posts

Back
Top Bottom