/ 猿问

# C#，关于DataTable的一个递归算法?

2019-01-27 09:05:37

# C#，关于DataTable的一个递归算法

## 3 回答

public DataTable dt={你内存的表};
public string nodes=string.Empty;
private void GetAllNodes(string ID)
{
DataRow[] drs=dt.Select("PID="+ID);
if (drs.Length <= 0) return;
foreach (DataRow dr in drs)
{
nodes+=dr["ID"].ToString()+"-";
GetAllNodes(dr["PID"].ToString());
}
}

//　给树添加根节点
private void AddRootNode(TreeView tv, DataTable dt)
{
if (dt != null && dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
if (dr["pid"] != null && dr["pid"].ToString().Length > 0)
{
TreeNode root = new TreeNode(dr["name"].ToString());
AppendChildNode(root, dt, dr["id"].ToString());
}
}
}
}

// 添加子节点
private void AppendChildNode(TreeNode node,DataTable dt, string id)
{
DataRow[] arrRows = dt.Select("pid='"+id+"'");
if (arrRows != null && arrRows.Length > 0)
{
foreach (DataRow dr in arrRows)
{
TreeNode tn = new TreeNode(dr["name"].ToString());
AppendChildNode(tn, dt, dr["id"].ToString());
}
}
}

• 3 回答
• 0 关注
• 242 浏览

0/150