数据库表滴结构是:(相当于一个树形结构)
id  parentid name
1    0       类型1
2    1       类型11
3    1       类型12
4    1       类型13
5    0       类型2
6    5       类型21
然后在程序中用Response.Write("<table></table>")滴方式根据数据库的表显示出来,
第一行提取滴是parentid为0的
第二行是根据第一行的id提取
依次类推.要注意合并单元格什么滴,由于数据库那张表是动态滴,用户可以随时增加,所以要用到递归吧什么滴来显示希望各位帅哥,美女帮帮忙咯 偶急死捏 不知道大家懂不懂喔滴意思 哎。

解决方案 »

  1.   

    为什么不直接用TreeView控件呢?递归是要用的,
      

  2.   

    因为需求要用table滴方式来显示.态提出表头,然后再根据表头去很多表提取不同数据,类似于报表滴意思哈
      

  3.   

    可以使用DataTable 构建个关系就可以满足你的要求。
      

  4.   

    不知道是否想要的是这种风格?我用gridView做的
    商品分类     上级分类         分类简介         操作 
    文具杂货(0)   文具      
    时尚数码(2)                 3216546546    
    数码相机(0)    时尚数码       123456      
    笔记本电脑(2)  时尚数码        123456     
    IBM(0)        笔记本电脑     法国风格      
    sony(0)       笔记本电脑     21354456      
      

  5.   

    不管是不是,给你一个做treeView和List的方法吧,对你或许有用 /// <summary>
        /// 使用TreeView控件,创建父节点的所有子节点方法,采用递归方法
        /// </summary>
        /// <param name="parentNode">TreeNode</param>
        /// <param name="dt">DataTable</param>
        /// <param name="target">string</param>
        /// <param name="url">string</param>
        private void CreateChildNode(TreeNode parentNode, DataTable dt, string target, string url)
        {
             //选择数据时,添加了排序表达式ShowOrder
            DataRow[] rowList = dt.Select("ParentID='" + parentNode.Value + "'","ShowOrder");
            foreach(DataRow row in rowList)
            {
                //创建新节点
                TreeNode node = new TreeNode();
                //设置节点的属性
                node.Text = row["Name"].ToString();
                node.Value = row["ID"].ToString();
                node.Target = target;
                node.NavigateUrl = url + node.Value;
                node.Expanded = true;
                parentNode.ChildNodes.Add(node);
                //递归调用,创建其他节点
                CreateChildNode(node, dt, target, url);
                if(node.ChildNodes.Count > 0)
                {
                    //选中节点时不进行事件处理
                    node.SelectAction = TreeNodeSelectAction.None;
                }
            }
        }    /// <summary>
        /// 使用TreeView控件显示分类层次
        /// </summary>
        /// <param name="tv">TreeView</param>
        public void InitCatalogTreeView(TreeView tv)
        {
            //1、调用GetCategorys()方法获取所有分类信息,并使用DataSet对象ds保存结果
            CategoryBLL cateBLL = new CategoryBLL();
            DataSet ds = cateBLL.GetCategorys();
            if(ds == null)
            {
                return;
            }
            if(ds.Tables.Count <= 0)
            {
                return;
            }        DataTable dt = ds.Tables[0];
            //2、清空树的所有节点
            tv.Nodes.Clear();
            DataRow[] rowList = dt.Select("ParentID=0","ShowOrder");
            if(rowList.Length < 1)
            {
                return;
            }
            for (int i = 0; i < rowList.Length;i++ )
            {
                //3、创建跟节点
                TreeNode root = new TreeNode();
                //设置根节点属性
                root.Text = rowList[i]["Name"].ToString();
                //设置根节点的Value值
                root.Value = rowList[i]["ID"].ToString();
                root.Target = "Product";
                root.NavigateUrl = "~/Product.aspx?CategoryID=" + root.Value;
                root.Expanded = true;
                //4、添加根节点
                tv.Nodes.Add(root);
                //5、创建其他节点
                CreateChildNode(root, dt, "Product", "~/Product.aspx?CategoryID=");
            }
            
        }
        /// <summary>
        /// 使用DropDownList控件显示分类层次结构
        /// </summary>
        /// <param name="list">ListControl</param>
        public void InitCategoryList(ListControl list)
        {
            //1、调用GetCategorys()方法获取所有分类信息,并使用DataSet对象保存结果
            CategoryBLL cateBLL = new CategoryBLL();
            DataSet ds = cateBLL.GetCategorys();
            if(ds == null)
            {
                return;
            }
            if(ds.Tables.Count <= 0)
            {
                return;
            }
            DataTable dt = ds.Tables[0];
            //2、清空树的所有节点
            list.Items.Clear();
            //3、获取一级数据项(ParentID域的值为0)的数据,如数据项的Names和Value属性的值
            DataRow[] rowList = dt.Select("ParentID = 0", "ShowOrder");
            if (rowList.Length < 1)
            {
                return;
            }
            string name = string.Empty;
            string value = string.Empty;
            foreach (DataRow row in rowList)
            {
                name = "|--" + row["Name"].ToString();
                value = row["ID"].ToString();
                list.Items.Add(new ListItem(name,value));
                CreateSubNode(list,dt,row["ID"].ToString(),name);
            }
        }
        /// <summary>
        /// 创建list的所有节点项
        /// </summary>
        /// <param name="list">ListControl</param>
        /// <param name="dt">DataTable</param>
        /// <param name="parentValue">string</param>
        /// <param name="parentName">string</param>
        private void CreateSubNode(ListControl list, DataTable dt, string parentValue, string parentName)
        {
            //1、从数据源dt中获取当前数据项的子数据项,并按照ShowOrder字段排序,获取的数据保存在rowList变量中
            DataRow[] rowList = dt.Select("ParentID=" + parentValue, "ShowOrder");
            //2、使用foreach语句处理rowList变量中的每一个数据项
            string name = string.Empty;
            string value = string.Empty;
            foreach(DataRow row in rowList)
            {
                name = parentName + "|--" + row["Name"].ToString();
                value = row["ID"].ToString();
                //3、为每一个数据项创建一个数据项ListItem,并设置每一个数据项的name和value属性的值,并添加到list控件中
                list.Items.Add(new ListItem(name, value));
                //4、递归调用,创建当前数据项的子数据项
                CreateSubNode(list, dt, row["ID"].ToString(), name);
            }
        }
      

  6.   

    html页面代码:<table runat="server" id="tblListView">
            <tr>
                <td style="width: 180px; text-align: center; height: 19px;">
                    <asp:HyperLink ID="srtUsername" runat="server">parentid</asp:HyperLink>
                </td>
                <td style="width: 100px; text-align: center; height: 19px;">
                    <asp:HyperLink ID="srtRole" runat="server">id</asp:HyperLink>
                </td>
                <td style="width: 100px; text-align: center; height: 19px;">
                    <asp:HyperLink ID="strState" runat="server">name</asp:HyperLink>
                </td>
            </tr>
            <tr>
            </tr>
        </table>
    后台放页面呈现代码:DataTable dt = 你取的数据源;
    foreach (DataRow row in dt.Rows)
    {
       HtmlTableRow tr = new HtmlTableRow();
       tr.Attributes.Add("class", "row");   HtmlTableCell tcparentid= new HtmlTableCell();
       tcparentid.InnerHtml = row["parentid"],      
       tr.Cells.Add(tcparentid);   HtmlTableCell tcID = new HtmlTableCell();
       tcID.InnerHtml = row["ID"].ToString();
       SetTableCell(tcID);
       tr.Cells.Add(tcID);   tblListView.Rows.Add(tr);
    }
    if (dt.Rows.Count > 0)
        tblListView.Rows.RemoveAt(1);
    dt.Dispose();
    是不是要这样的效果?
      

  7.   

    需要用到的方法和存储过程:
     /// <summary>
            /// 获取所有分类信息
            /// </summary>
            /// <returns>DataSet</returns>
            public DataSet GetCategorys()
            {
                string connectionString = ConfigurationManager.ConnectionStrings["userConnectionString"].ConnectionString;
                //创建连接
                SqlConnection con = new SqlConnection(connectionString);
                //设置被执行的存储过程
                string cmdText = "pr_GetCategorys";           
                //创建SqlDataAdapter
                SqlDataAdapter da = new SqlDataAdapter(cmdText, con);
                //设置执行方式为存储过程
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                //定义DataSet
                DataSet ds = new DataSet();
                try
                {
                    //打开连接
                    con.Open();
                    //填充数据
                    da.Fill(ds, "DataTable");
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message, ex);
                }
                finally
                {
                    //关闭连接
                    con.Close();
                }
                //返回DataSet
                return ds;
            }存储过程:
    SET QUOTED_IDENTIFIER ON 
    GO
    SET ANSI_NULLS ON 
    GO--查询所有商品分类
    create procedure pr_GetCategorysASSELECT
    A.ID,
    A.Name,
    A.ParentID,
    A.ShowOrder,
    A.Re,
    ISNULL((SELECT Name FROM Category AS B WHERE A.ParentID = B.ID),null) AS ParentName,
    ISNULL((SELECT COUNT(*) FROM Category AS C WHERE A.ID = c.ParentID),0) AS SubCount,
    ISNULL((SELECT COUNT(*) FROM Category AS D WHERE A.ParentID = D.ParentID),0) AS SiblingCount

    FROM
    Category AS AORDER BY
    ParentID,ShowOrder 
    GO
    SET QUOTED_IDENTIFIER OFF 
    GO
    SET ANSI_NULLS ON 
    GO
      

  8.   


     Sub selectDB()
               Dim sbTable As New StringBuilder        cmd = conndb.CreateCommand
            cmd.CommandText = "SELECT [my].ID, [my].[mdate], [my].cmoney, income.cd, [my].pmoney, paybig.paybigname, paysmall.paysmallname FROM (paysmall LEFT JOIN paybig ON paysmall.paybigid=paybig.paybigid) RIGHT JOIN (my LEFT JOIN income ON [my].cid=income.cid) ON (paysmall.paybigid=[my].paybigid) AND (paysmall.paysmallid=[my].paysmallid)WHERE 1=1 ORDER BY [my].[mdate];"
            conndb.Open()
            Dim dr As OleDb.OleDbDataReader = cmd.ExecuteReader
           sbTable.Append("<table cellSpacing=""1""cellPadding=""1"" border=""1""><tr>")
            sbTable.Append("<td >Id</td><td>日期</td><td>收入金额</td><td>收入区分</td><td>支出金额</td><td>支出区分</td><td>支出详细区分</td></tr>")
           While (dr.Read())
                sbTable.Append("<tr><td>")
                sbTable.Append(dr("ID").ToString())
                sbTable.Append("</td><td>")
                sbTable.Append(dr("mdate").ToString().Substring(0, 10))
                sbTable.Append("</td><td>")
                sbTable.Append(dr("cmoney").ToString())
                sbTable.Append("</td><td>")
                sbTable.Append(dr("cd").ToString())
                sbTable.Append("</td><td>")
                sbTable.Append(dr("pmoney").ToString())
                sbTable.Append("</td><td>")
                sbTable.Append(dr("paybigname").ToString())
                sbTable.Append("</td><td>")
                sbTable.Append(dr("paysmallname").ToString())            sbTable.Append("</td><td><")
                ' sbTable.Append(dr("paysmallname").ToString())
            End While
            sbTable.Append("</td><tr>")
            sbTable.Append("</tr></table>")        dr.Close()
            conndb.Close()
      

  9.   

    using System;
    using System.Data;
    using System.Configuration;
    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.Collections;
    using System.Collections.Generic;
    using System.Text;public abstract class Component
    {
        public string strName;    public Component(string name, int Level)
        {
            strName = name;
            this.Level = Level;
        }
        public List<Component> ComponentList = new List<Component>();
        abstract public void Add(Component c);    public abstract StringBuilder DumpContents();
        public int Level = 0;
        public int NodeNum = 0;
        public Component ParentNode = null;
    }public class Composite : Component
    {    public Composite(string s, int Level) : base(s, Level) { }    override public void Add(Component c)
        {
            ComponentList.Add(c);
            if (c is Leaf)
            {
                Component p = c.ParentNode;
                while ((p) != null)
                {
                    p.NodeNum += 1;
                    p = p.ParentNode;
                }
            }
        }    public override StringBuilder DumpContents()
        {
            System.Web.HttpContext.Current.Response.Write("Node: " + strName + "========"+this.NodeNum+"<br>");
            StringBuilder sb = new StringBuilder();
      
            sb.Append("<td colspan='" + this.NodeNum + "'>" + this.strName + "</td>");   
        
            return sb;
        }
       
    }public class Leaf : Component
    {
        public Leaf(string s,int Level) : base(s,Level) { }    override public void Add(Component c)
        {
            Console.WriteLine("Cannot add to a leaf");
           
        }    public override StringBuilder DumpContents()
        {
            System.Web.HttpContext.Current.Response.Write("----leaf: " + strName + "<br>");
            StringBuilder sb = new StringBuilder();
            sb.Append("<td>"+this.strName+"</td>");
            return sb;
        }
    } protected void Page_Load(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            using (SqlConnection cn = new SqlConnection("server=.;uid=sa;pwd=123;database=aaa"))
            {
                cn.Open();
                SqlCommand cmd = new SqlCommand("select * from Report_Auxiliary");
                cmd.Connection = cn;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
               
                da.Fill(ds);
                cn.Close();
            }        Component root = new Composite("root", 0);
            CreateTree(root, 0, ds,0);
            CreateBlankCell(root);
            StringBuilder sb = new StringBuilder();
          
             PrintTable(root);       
             sb.Append("<table border='1' bordercolor='red'>");
          foreach(StringBuilder s in listTR)
          {
              sb.Append("<tr>");
              sb.Append(s);
              sb.Append("</tr>");
          }
           sb.Append("</table>");
           Response.Write(sb.ToString());
        }    void CreateBlankCell(Component root)
        {
            for (int i = 0; i < root.ComponentList.Count; i++)
            {
                if (root.ComponentList[i].ComponentList.Count == 0)
                {
                    if(!(root.ComponentList[i] is Leaf))
                    {
                        Component cc = new Leaf("&nbsp;", root.ComponentList[i].Level + 1);
                        cc.ParentNode = root.ComponentList[i];
                        root.ComponentList[i].Add(cc);
                    }
                }
                CreateBlankCell(root.ComponentList[i]);
            }
        }    void CreateTree(Component root, int parentid, DataSet ds, int Level)
        {
            DataView dv = new DataView(ds.Tables[0]);
            dv.RowFilter = "parent_id=" + parentid;
            int lv = Level+1;
            for (int i = 0; i < dv.Count; i++)
            {
                if ((int)dv[i]["HaveSon"] == 1)
                {
                    Component child = new Composite(dv[i]["Item_name"].ToString(), lv);
                    child.ParentNode = root;
                    root.Add(child);
                   
                    CreateTree(child, (int)dv[i]["id"], ds,lv);
                }
                else
                {
                    Component leaf = new Leaf(dv[i]["Item_name"].ToString(), lv);
                    leaf.ParentNode = root;
                    root.Add(leaf);
                   
                }
            }
        }
        List<StringBuilder> listTR = new List<StringBuilder>();
        void PrintTable(Component root)
        {
           
            foreach (Component c in root.ComponentList)
            {
                if (c.Level > listTR.Count)
                {
                    listTR.Add(new StringBuilder());
                }
                listTR[c.Level-1].Append(c.DumpContents());
                PrintTable(c);
            }
        }