private DataTable _customsTable; //绑定treeView的数据源
private string _cycleCloumn; //递归列
/// <summary>
/// 绑定根节点
/// </summary>
/// <param name="tv"></param>
/// <param name="source"></param>
/// <param name="cycleCloumn"></param>
private void BindTree(TreeView tv, DataTable source, string cycleCloumn)
{
_customsTable = source;
_cycleCloumn = cycleCloumn;
CreateChildTree(tv.Nodes, "0");
}
/// <summary>
///
/// </summary>
/// <param name="parentNode"></param>
/// <param name="parentID"></param>
private void CreateChildTree(TreeNodeCollection parentNode, string parentID)
{
DataView dv = _customsTable.DefaultView;
if (_customsTable.Rows.Count == 0)
{
return;
}
dv.RowFilter = _cycleCloumn + "= '" + parentID + "'";
DataTable dt = dv.ToTable();
foreach (DataRow item in dt.Rows)
{
var node = new TreeNode();
node.Value = item["id"].ToString();
node.Text = item["name"].ToString();
node.Target = item["code"].ToString();
parentNode.Add(node);
//递归出子节点
CreateChildTree(node.ChildNodes, item["id"].ToString());
}
}