Winfrom TreeView 无限分级
public void AddTreeView(string ParentID, TreeNode pNode, DataSet set)
{
string strName = "N_Name"; // 数据库名字字段
string strID = "U_ID"; // 数据库ID字段
string strParentID = "N_PaterId"; // 数据库父级ID字段
DataTable dt = set.Tables[0];
DataView dvTree = new DataView(dt); //添加数据源
dvTree.RowFilter = "N_PaterId = '" + ParentID + "'"; //过滤节点条件
foreach (DataRowView Row in dvTree)
{
TreeNode Node = new TreeNode();
if (pNode == null)
{
Node.Text = Row[strName].ToString();
Node.Name = Row[strName].ToString();
Node.Tag = Row[strID].ToString();
Node.ImageIndex = 1;
this.treeView1.Nodes.Add(Node);
AddTree(Row[strID].ToString(), Node, set); //再次递归
}
else
{
Node.Text = Row[strName].ToString();
Node.Name = Row[strName].ToString();
Node.Tag = Row[strID].ToString();
Node.ImageIndex = 1;
pNode.Nodes.Add(Node);
AddTree(Row[strID].ToString(), Node, set); //再次递归
}
}
}