Tree TreeView control to interact with the DataTable to add nodes (most efficient method)

#region "tree node read from the Datatable"
/// <Summary>
read /// tree node from the Datatable "
/// </ Summary>
/// <param name =" of TreeView1 "> filled in TreeView control </ param>
/// <param name = "DT"> data source the DataTable </ param>
/// <param name = "IsAppendNode"> is added to the existing nodes TreeView control, add or clear < / param>
/// <param name = "ParentNumberColumnIndex"> in a DataTable, representatives of the parent node ID column index </ param>
/// <param name = "NumberColumnIndex"> in DataTable, the node ID representing the current column index </ param>
/// <param name = "NameColumnIndex"> in a DataTable representing the current node name column index </ param>
/// <Returns> True / False </ Returns>
public BOOL ReadNodesFromDataTable (the TreeView of TreeView1 , DataTable DT,bool IsAppendNode, int ParentNumberColumnIndex, int NumberColumnIndex, int NameColumnIndex)
{
try
{
if (IsAppendNode == false)
{
TreeView1.Nodes.Clear();
}
if (DT != null && DT.Rows.Count > 0)
{
DataRow[] DR = null;
DR = DT.Select(DT.Columns[ParentNumberColumnIndex].ColumnName + "='' or " + DT.Columns[ParentNumberColumnIndex].ColumnName + "='0' or " + DT.Columns[ParentNumberColumnIndex].ColumnName + " is null");//先将顶级的查出来
for (int I = 0; I <= DR.Length - 1; I++)//先将顶级的加入到TreeView中
{
TreeNode TNode = new TreeNode(DR[I][DT.Columns[NameColumnIndex].ColumnName].ToString());
TNode.Tag = DR[I][DT.Columns[NumberColumnIndex].ColumnName].ToString();
TNode.Name = DR[I][DT.Columns[NameColumnIndex].ColumnName].ToString();
TreeView1.Nodes.Add(TNode);
}
for (int I = 0; I <= TreeView1.Nodes.Count - 1; I ++) // recursively traversing nodes
{
ForTreeNodeFormDT (TreeView1.Nodes [the I], DT, ParentNumberColumnIndex, NumberColumnIndex, NameColumnIndex);
}
}
return to false ;
}
the catch
{
return to true;
}
}

/// <Summary>
/// DT from the recursive traversal node
/// </ Summary>
/// <param name = "tempNode"> incoming top node </ param>
/// <param name = "DT"> save the configuration of the TreeView DataTable </ param>
/// <param name = "ParentNumberColumnIndex"> in a DataTable, representatives of the parent node ID column index </ param>
/// <param name = "NumberColumnIndex" > in DataTable, the node ID representing the current column index </ param>
/// <param name = "NameColumnIndex "> in a DataTable representing the current column index node names </ param>
private void ForTreeNodeFormDT(TreeNode TempNode, DataTable DT, int ParentNumberColumnIndex, int NumberColumnIndex, int NameColumnIndex)
{
string TTag = null;
TTag = TempNode.Tag.ToString();
DataRow[] DR = null;
DR = DT.Select(DT.Columns[ParentNumberColumnIndex].ColumnName + "='" + TTag + "'");

for (int I = 0; I <= DR.Length - 1; I++)
{
TreeNode TNode = new TreeNode(DR[I][DT.Columns[NameColumnIndex].ColumnName].ToString());
TNode.Tag = DR[I][DT.Columns[NumberColumnIndex].ColumnName].ToString();
TNode.Name = DR[I][DT.Columns[NameColumnIndex].ColumnName].ToString();
TempNode.Nodes.Add(TNode);
}

foreach (TreeNode aNode in TempNode.Nodes)
{
ForTreeNodeFormDT(aNode, DT, ParentNumberColumnIndex, NumberColumnIndex, NameColumnIndex);
}
}
#endregion

Guess you like

Origin www.cnblogs.com/blogpro/p/11462918.html