表结构如下id,name,fatherCode,orderid
id 表的主键 name 名称 fatherCode 上一级代码 orderid 排序ID其中 fatherCode 如果是最高级别那么该字段数据为0,否则就是想对应的ID
问题:怎么让他们主从显示。2 电脑 2 0
12 神州 1 2
4 联想 2 2
6 戴尔 3 2
8 IBM 4 2
9 化妆品 3 0
10 丁家宜 1 9
id 表的主键 name 名称 fatherCode 上一级代码 orderid 排序ID其中 fatherCode 如果是最高级别那么该字段数据为0,否则就是想对应的ID
问题:怎么让他们主从显示。2 电脑 2 0
12 神州 1 2
4 联想 2 2
6 戴尔 3 2
8 IBM 4 2
9 化妆品 3 0
10 丁家宜 1 9
http://topic.csdn.net/u/20080407/14/8c53338b-6435-4f5c-a5fe-c304c0644c0b.html
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form id="form1" runat="server">
<asp:TreeView ID="TreeView1" runat="server"></asp:TreeView>
</form>
</body>
</html>
<script runat="server">
void Page_Load1(object sender, EventArgs e)
{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter("select * from 表名", "server=xxx;uid=sa;pwd=xx;database=xxx");
sda.Fill(dt); Func<int, IEnumerable<TreeNode>> selectNodes = null;
selectNodes = parentId =>
dt.Rows.OfType<DataRow>()
.Where(row => row["fatherCode"].Equals(parentId))
.OrderBy(row => row["orderid"])
.Select(row =>
{
var node = new TreeNode(row["name"].ToString(), row["id"].ToString());
selectNodes(Convert.ToInt32(row["id"])).ToList().ForEach(n => node.ChildNodes.Add(n));
return node;
}); selectNodes(0).ToList().ForEach(node => TreeView1.Nodes.Add(node));
}
</script>