1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TreeView
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//PopulateTreeView
PopulateTreeView();
}
/// <summary>
/// Allow you to retrieve data to display in TreeView
/// </summary>
private void PopulateTreeView()
{
//Create datacontext object
TreeViewContextDataContext ctx = new TreeViewContextDataContext();
//Using linq query to prepare data
var usines = (from usine in ctx.Ensemble_Usines
join s_usine in ctx.Sous_Ensemble_Usines
on usine.Id equals s_usine.Id_Ensemble_Usine
into JoinSusine
from s_usineJoin in JoinSusine.DefaultIfEmpty()
join partLib in ctx.Partie_Libelles
on s_usineJoin.Id equals partLib.Id_Sous_Ensemble_Usine
into JoinUsinXPartLib
from UsinXPartLib in JoinUsinXPartLib.DefaultIfEmpty()
join lib in ctx.Libelles
on UsinXPartLib.Id equals lib.Id_Partie_Libelle
into JoinPartLibLib
from usineNode in JoinPartLibLib.DefaultIfEmpty()
select new
{
Usine_Id = usine.Id,
Usine_Name = usine.Name,
S_usines = s_usineJoin,
PartUsines = UsinXPartLib,
PartLibXLib = usineNode
}).ToList();
//take position like tree
var treeGrouped = from l in usines
group l by l.Usine_Name into groupParentID
select new
{
Name = groupParentID.Key,
Items = from ens_us in groupParentID
where ens_us.S_usines != null
group ens_us by ens_us.S_usines.Name into sousEnsUsineGroup
select new
{
IdSousUsine = sousEnsUsineGroup.Key,
Items = from part_lib in sousEnsUsineGroup
where part_lib.PartUsines != null
group part_lib by part_lib.PartUsines.Name into groupPartLib
select new
{
NamePartLib = groupPartLib.Key,
Items = from li in groupPartLib
select li.PartLibXLib.Name
},
},
};
//Binding
TreeNode parentNode = null;
foreach (var usineParent in treeGrouped)
{
parentNode = new TreeNode(usineParent.Name);
foreach (var s_usine_node in usineParent.Items)
{
TreeNode usineChildNode = new TreeNode(s_usine_node.IdSousUsine.ToString());
foreach (var partLib in s_usine_node.Items)
{
TreeNode partLibNode = new TreeNode(partLib.NamePartLib);
foreach (var lib in partLib.Items)
{
TreeNode childLib = new TreeNode(lib.ToString());
partLibNode.Nodes.Add(childLib);
}
usineChildNode.Nodes.Add(partLibNode);
}
parentNode.Nodes.Add(usineChildNode);
}
parentNode.Collapse();
treeView1.Nodes.Add(parentNode);
}
}
}
} |
Partager