TreeView数据递归显示

/// <summary>
/// 显示药品类别列表
/// </summary>
/// <param name="tv"></param>
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();//展开所有树节点
}

/// <summary>
/// 新增子节点
/// </summary>
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数据递归显示



posted @ 2012-05-27 10:57  风与雨无阻  阅读(204)  评论(0编辑  收藏  举报