TreeView数据递归显示

///


/// 显示药品类别列表
///

///
public void SetCommondCategory(TreeView tv)
{
    //先清除原有的节点
    tv.Nodes.Clear();
    sb.Remove(0, sb.Length);
    sb.Append("select categoryName,parentCategoryNumber,parentCategoryNumber from t_drugCategory  where parentCategoryNumber like '__' ");
    dt = DBCommon.ExecuteDataTable(sb.ToString());
    foreach (DataRow row in dt.Rows)
    {
        TreeNode node = new TreeNode();
        node.Text = row["categoryName"].ToString();
        node.Tag = row["parentCategoryNumber"].ToString();
        tv.Nodes.Add(node);//添加父节点
        TreeViewNodeAdd(node);
    }   

    tv.ExpandAll();//展开所有树节点
}

///


/// 新增子节点
///

private void TreeViewNodeAdd(TreeNode node)
{
    DataTable dt2 = new DataTable();
    sb.Remove(0, sb.Length);
    sb.Append(" select categoryName,parentCategoryNumber from t_drugCategory where parentCategoryNumber like '" + node.Tag + "__' ");
    dt2 = DBCommon.ExecuteDataTable(sb.ToString());
    for (int i = 0; i < dt2.Rows.Count; i++)
    {
        TreeNode newNode = new TreeNode();
        newNode.Text = dt2.Rows[i]["categoryName"].ToString();
        newNode.Tag = dt2.Rows[i]["parentCategoryNumber"].ToString();
        node.Nodes.Add(newNode);
        TreeViewNodeAdd(newNode);
    }
}

数据表格式:

TreeView数据递归显示_第1张图片



转载于:https://www.cnblogs.com/pyq228/archive/2012/05/27/2519929.html

你可能感兴趣的:(TreeView数据递归显示)