Ciao, ho creato un TreeView che dovrebbe visualizzarmi una lista di articoli partendo da una tabella di questo tipo
Tabella Articoli
ID
1
2
3
4
5
6
7
IDArticoli - IDComponenti
1 - 2
1 - 3
1 - 4
2 - 1
4 - 6
Quindi l'articolo 1 è composto da 2,3,4 ed è componente dell'articolo 2... ecc...
io vorrei visualizzare ad albero questa struttura che potrebbe essere ad n livelli
Ho scaricato una classe che dovrebbe semplificare questa gestione, ma non capisco come gestire la mia situazione ad n livelli... nell'esempio del codice che ho trovato viene fatto:
this.bookTree.AutoBuildTree = false;
this.bookTree.SetLeafData("Title", "title", "title_id", 2, 2);
this.bookTree.DataSource = MioDataView...
this.bookTree.AddGroup("Publisher", "pub_id", "pub_name", "pub_id", 0, 0);
this.bookTree.AddGroup("Author", "au_id", "au_name", "au_id", 1, 3);
this.bookTree.BuildTree();
Grazie per l'aiuto
La classe (se può interessare a qualcuno dato che è ben fatta) è la seguente:
public class dbTreeViewCtrl : System.Windows.Forms.TreeView
{
private System.ComponentModel.Container components = null;
public dbTreeViewCtrl()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// TODO: Add any initialization after the InitComponent call
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
#region Component Designer generated code
[System.Diagnostics.DebuggerStepThrough()]
private void InitializeComponent()
{
//
// MyTreeViewCtrl
//
components = new Container ();
}
#endregion
protected override void OnPaint(PaintEventArgs pe)
{
// TODO: Add custom paint code here
// Calling the base class OnPaint
base.OnPaint(pe);
}
private bool m_autoBuild = true;
public bool AutoBuildTree
{
get
{
return this.m_autoBuild;
}
set
{
this.m_autoBuild = value;
}
}
#region Data Binding
private CurrencyManager m_currencyManager = null;
private String m_ValueMember;
private String m_DisplayMember;
private object m_oDataSource;
[Category("Data")]
public object DataSource
{
get
{
return m_oDataSource;
}
set
{
if ( value == null )
{
this.m_currencyManager = null;
this.Nodes.Clear();
}
else
{
if ( !(value is IList || m_oDataSource is IListSource ) )
throw (new System.Exception ("Invalid DataSource"));
else
{
if ( value is IListSource )
{
IListSource myListSource = (IListSource) value;
if ( myListSource.ContainsListCollection == true )
throw (new System.Exception ("Invalid DataSource"));
}
this.m_oDataSource = value;
this.m_currencyManager = (CurrencyManager) this.BindingContext[value];
if ( this.AutoBuildTree )
BuildTree();
}
}
}
} // end of DataSource property
[Category("Data")]
public string ValueMember
{
get
{
return this.m_ValueMember;
}
set
{
this.m_ValueMember = value;
}
}
[Category("Data")]
public string DisplayMember
{
get
{
return this.m_DisplayMember;
}
set
{
this.m_DisplayMember = value;
}
}
public object GetValue(int index)
{
IList innerList = this.m_currencyManager.List;
if ( innerList != null )
{
if ( (this.ValueMember != "") && (index >= 0 && 0 < innerList.Count))
{
PropertyDescriptor pdValueMember;
pdValueMember = this.m_currencyManager.GetItemProperties()[this.ValueMember];
return pdValueMember.GetValue(innerList[index]);
}
}
return null;
}
public object GetDisplay(int index)
{
IList innerList = this.m_currencyManager.List;
if ( innerList != null )
{
if ( (this.DisplayMember != "") && (index >= 0 && 0 < innerList.Count))
{
PropertyDescriptor pdDisplayMember;
pdDisplayMember= this.m_currencyManager.GetItemProperties()[this.ValueMember];
return pdDisplayMember.GetValue (innerList[index]);
}
}
return null;
}
private ArrayList treeGroups = new ArrayList();
public void BuildTree()
{
this.Nodes.Clear();
if ( (this.m_currencyManager != null) && (this.m_currencyManager.List != null) )
{
IList innerList = this.m_currencyManager.List;
TreeNodeCollection currNode = this.Nodes;
int currGroupIndex = 0;
int currListIndex = 0;
if ( this.treeGroups.Count > currGroupIndex )
{
Group currGroup = (Group) treeGroups[currGroupIndex];
dbTreeNode myFirstNode = null;
PropertyDescriptor pdGroupBy;
PropertyDescriptor pdValue;
PropertyDescriptor pdDisplay;
pdGroupBy = this.m_currencyManager.GetItemProperties ()[currGroup.GroupBy];
pdValue = this.m_currencyManager.GetItemProperties()[currGroup.ValueMember];
pdDisplay = this.m_currencyManager.GetItemProperties()[currGroup.DisplayMember];
string currGroupBy = null;
if ( innerList.Count > currListIndex )
{
object currObject;
while (currListIndex < innerList.Count)
{
currObject = innerList[currListIndex];
if ( pdGroupBy.GetValue(currObject).ToString() != currGroupBy )
{
currGroupBy = pdGroupBy.GetValue(currObject).ToString();
myFirstNode = new dbTreeNode (currGroup.Name,
pdDisplay.GetValue (currObject).ToString(),
currObject,
pdValue.GetValue(innerList[currListIndex]),
currGroup.ImageIndex,
currGroup.SelectedImageIndex,
currListIndex);
currNode.Add ((TreeNode) myFirstNode);
}
else
AddNodes (currGroupIndex, ref currListIndex, myFirstNode.Nodes, currGroup.GroupBy);
} // end while
} // end if
} // end if
else
{
while (currListIndex < innerList.Count )
{
AddNodes (currGroupIndex, ref currListIndex, this.Nodes, "");
}
} // end else
if ( this.Nodes.Count > 0 )
this.SelectedNode = this.Nodes[0];
} // end if
}
private void AddNodes(int currGroupIndex,
ref int currentListIndex,
TreeNodeCollection currNodes,
String prevGroupByField)
{
IList innerList = this.m_currencyManager.List;
System.ComponentModel.PropertyDescriptor pdPrevGroupBy = null;
string prevGroupByValue = null;;
Group currGroup;
if ( prevGroupByField != "" )
pdPrevGroupBy = this.m_currencyManager.GetItemProperties()[prevGroupByField];
currGroupIndex += 1;
if ( treeGroups.Count > currGroupIndex )
{
currGroup = ( Group) treeGroups[currGroupIndex];
PropertyDescriptor pdGroupBy = null;
PropertyDescriptor pdValue = null;
PropertyDescriptor pdDisplay = null;
pdGroupBy = this.m_currencyManager.GetItemProperties()[currGroup.GroupBy];
pdValue = this.m_currencyManager.GetItemProperties()[currGroup.ValueMember];
pdDisplay = this.m_currencyManager.GetItemProperties()[currGroup.DisplayMember];
string currGroupBy = null;
if ( innerList.Count > currentListIndex )
{
if ( pdPrevGroupBy != null )
prevGroupByValue = pdPrevGroupBy.GetValue(innerList[currentListIndex]).ToString();
dbTreeNode myFirstNode = null;
object currObject = null;
while ( (currentListIndex < innerList.Count) &&
(pdPrevGroupBy != null) &&
(pdPrevGroupBy.GetValue(innerList[currentListIndex]).ToString() == prevGroupByValue) )
{
currObject = innerList[currentListIndex];
if ( pdGroupBy.GetValue (currObject).ToString() != currGroupBy )
{
currGroupBy = pdGroupBy.GetValue(currObject).ToString();
myFirstNode = new dbTreeNode (currGroup.Name,
pdDisplay.GetValue (currObject).ToString(),
currObject,
pdValue.GetValue(innerList[currentListIndex]),
currGroup.ImageIndex,
currGroup.SelectedImageIndex,
currentListIndex);
currNodes.Add( (TreeNode) myFirstNode );
}
else
AddNodes(currGroupIndex, ref currentListIndex, myFirstNode.Nodes, currGroup.GroupBy);
}
}
}
else
{
dbTreeNode myNewLeafNode;
object currObject = this.m_currencyManager.List[currentListIndex];
if ( (this.DisplayMember != null) && (this.ValueMember != null) &&
(this.DisplayMember != "") && (this.ValueMember != "") )
{
PropertyDescriptor pdDisplayloc =
this.m_currencyManager.GetItemProperties()[this.DisplayMember];
PropertyDescriptor pdValueloc =
this.m_currencyManager.GetItemProperties()[this.ValueMember];
myNewLeafNode = new dbTreeNode (this.Tag == null ? "" : this.Tag.ToString(),
pdDisplayloc.GetValue(currObject).ToString(),
currObject,
pdValueloc.GetValue(currObject),
currentListIndex);
}
else
myNewLeafNode = new dbTreeNode ("", currentListIndex.ToString(),
currObject,
currObject,
this.ImageIndex, this.SelectedImageIndex,
currentListIndex);
currNodes.Add( (TreeNode) myNewLeafNode);
currentListIndex += 1;
}
}
#endregion
#region Groups
public void AddGroup(String name, String groupBy, String displayMember,
String valueMember, int imageIndex, int selectedImageIndex)
{
Group myNewGroup = new Group (name, groupBy, displayMember, valueMember,
imageIndex, selectedImageIndex);
this.AddGroup (myNewGroup);
}
public Group[] GetGroups()
{
return ( (Group[]) treeGroups.ToArray (Type.GetType("Group")));
}
#endregion
public void SetLeafData(String name, String displayMember, String valueMember,
int imageIndex, int selectedImageIndex)
{
this.Tag = name;
this.DisplayMember = displayMember;
this.ValueMember = valueMember;
this.ImageIndex = imageIndex;
this.SelectedImageIndex = selectedImageIndex;
}
public bool IsLeafNode (TreeNode node)
{
return (node.Nodes.Count == 0);
}
#region Keeping Everything In Sync
public TreeNode FindNodeByValue (object value)
{
return FindNodeByValue (value, this.Nodes);
}
protected override void OnAfterSelect(TreeViewEventArgs e)
{
dbTreeNode leafNode = (dbTreeNode) e.Node;
if (leafNode != null)
{
if ( this.m_currencyManager.Position != leafNode.Position )
this.m_currencyManager.Position = leafNode.Position;
}
// TODO: Add MyTreeViewCtrl.OnAfterSelect implementation
base.OnAfterSelect (e);
}
#endregion
}
public class Group
{
private String groupName;
private String groupByMember;
private String groupByDisplayMember;
private String groupByValueMember;
private int groupImageIndex;
private int groupSelectedImageIndex;
public Group (String name, String groupBy, String displayMember,
String valueMember, int imageIndex, int selectedImageIndex)
{
this.ImageIndex = imageIndex;
this.Name = name;
this.GroupBy = groupBy;
this.DisplayMember = displayMember;
this.ValueMember = valueMember;
this.SelectedImageIndex = selectedImageIndex;
}
public Group (String name, String groupBy, String displayMember,
String valueMember, int imageIndex) :
this (name, groupBy, displayMember, valueMember, imageIndex, imageIndex)
{
}
public Group (String name, String groupBy) :
this (name, groupBy, groupBy, groupBy, -1, -1)
{
}
...
}
public class dbTreeNode : TreeNode
{
private String m_groupName;
private object m_value;
private object m_item;
private int m_position;
public dbTreeNode ()
{
}
public dbTreeNode (String groupName, String text, object item, object value,
int imageIndex, int selectedImgIndex, int position)
{
this.GroupName = groupName;
this.Text = text;
this.Item = item;
this.Value = value;
this.ImageIndex = imageIndex;
this.SelectedImageIndex = selectedImgIndex;
this.m_position = position;
}
public dbTreeNode (String groupName, String text, object item, object value, int position)
{
this.GroupName = groupName;
this.Text = text;
this.Item = item;
this.Value = value;
this.m_position = position;
}

Rispondi quotando