前台输出的样式
前台象这样的输出球类
——篮球
——足球
音乐
——钢琴
——小替琴
手机
要求:有小类,大类不连接,无小类,大类可连接。
数据库是这么设计的:
ID CategroyName parentsID
1 球类 0
2 音乐 0
3 手机 0
4 篮球 1
5 足球 1
6 钢琴 2
6 小提琴 2
请帮帮忙?速要的啊。。 --------------------编程问答--------------------
DataTable dt = ....;--------------------编程问答-------------------- 我有这种控件。 给分我 给控件你 --------------------编程问答--------------------
TreeView treeView1;
Page_Load()
{
TreeNode root = new TreeNode()
root.Text="root";
int areaID=0;
treeView1.Node.Add( root);
createSubNode( root,areaID)
}
protected void createSubNode( TreeNode _Node,int _AreaID)
{
DataRow[] drs = dt.Select("所属区域=" + _AreaID.ToString());
foreach(DataRow dr in drs)
{
TreeNode node=new TreeNode();
node.Text=dr["地区名"];
......
int areaID = Convert.ToInt32( dr["地区ID"]);
_Node.ChildNodes.Add(node);
createSubNode( node,areaID)
}
}
//仅供参考
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.InitTree();
}
private DataView GetData()
{
DataTable table = new DataTable();
table.Columns.Add("CID", typeof(int));
table.Columns.Add("CName", typeof(string));
table.Columns.Add("ParentID", typeof(int));
DataRow row0 = table.NewRow();
DataRow row1 = table.NewRow();
DataRow row2 = table.NewRow();
DataRow row3 = table.NewRow();
DataRow row4 = table.NewRow();
DataRow row5 = table.NewRow();
DataRow row6 = table.NewRow();
DataRow row7 = table.NewRow();
DataRow row8 = table.NewRow();
DataRow row9 = table.NewRow();
row0.ItemArray = new object[] { 1, ".net", 0 };
row1.ItemArray = new object[] { 2, "c#", 1 };
row2.ItemArray = new object[] { 3, "asp.net", 1 };
row3.ItemArray = new object[] { 4, "office", 0 };
row4.ItemArray = new object[] { 5, "word", 4 };
row5.ItemArray = new object[] { 6, "excel", 5 };
row6.ItemArray = new object[] { 7, "os", 0 };
row7.ItemArray = new object[] { 8, "unix", 7 };
row8.ItemArray = new object[] { 9, "linux", 8 };
row9.ItemArray = new object[] { 10, "c#.net", 2 };
table.Rows.Add(row0);
table.Rows.Add(row1);
table.Rows.Add(row2);
table.Rows.Add(row3);
table.Rows.Add(row4);
table.Rows.Add(row5);
table.Rows.Add(row6);
table.Rows.Add(row7);
table.Rows.Add(row8);
table.Rows.Add(row9);
return table.DefaultView;
}
public void InitTree()
{
DataView dv = this.GetData();
dv.RowFilter = "ParentID=0";
foreach (DataRowView drv in dv)
{
TreeNode node = new TreeNode();
node.Text = drv["cname"].ToString();
node.Value = drv["cid"].ToString();
node.Expanded = true;
this.TreeView1.Nodes.Add(node);
BuildTree(dv, node);
}
}
private void BuildTree(DataView dv, TreeNode pnode)
{
dv.RowFilter = "[ParentID]=" + pnode.Value;
foreach (DataRowView drv in dv)
{
string name = drv["cname"].ToString();
TreeNode cnode = new TreeNode(name);
cnode.Value = drv["cid"].ToString();
if (cnode.Value != pnode.Value)
{
pnode.ChildNodes.Add(cnode);
BuildTree(dv, cnode);
}
}
}
}
}
--------------------编程问答-------------------- 嵌套rep也能实现简单 --------------------编程问答-------------------- 先取出parentsID =0的id//取出大类
判断表中有没有parentsID =id的记录
如果有则不可以链接,没有的话就可以 --------------------编程问答--------------------
用TreeView实现 --------------------编程问答-------------------- 不知道是不是这样
select test.CategroyName,test1.CategroyName
from test
left join
(select * from dbo.test
where parentsID>0
) as test1
on test.ID=test1.parentsID
where test.parentsID=0
补充:.NET技术 , ASP.NET