我现在有 一张数据库的表Functid(int)  name(nvarchar)  function(nvarchar)     ction(bit)
1           aaa                   数学                   0
2           aaa                   英文                   0
3           aaa                   语文                   0
4           aaa                   化学                   1
5           aaa                   物理                   0
6           bbb                   英文                   0
7           bbb                   语文                   0
8           bbb                   数学                   1
9           bbb                   物理                   110          ccc                   语文                   0
11          ccc                   数学                   012          eee                   语文                   1
13          eee                   数学                   1
14          eee                   英文                   1
我希望 用datalist 或者用repeater 来显示 ···aaa    数学    英文    语文    化学   物理 
0       0       0        1      0 
bbb    英文    语文    数学    物理
        0        0      1        1
ccc    语文    数学
        0        0
eee    语文    数学    英文
        1       1        1

解决方案 »

  1.   

    id(int)  name(nvarchar)  function(nvarchar)     ction(bit)
    1           aaa                   数学                   0
    2           aaa                   英文                   0
    3           aaa                   语文                   0
    4           aaa                   化学                   1
    5           aaa                   物理                   0
    6           bbb                   英文                   0
    7           bbb                   语文                   0
    8           bbb                   数学                   1
    9           bbb                   物理                   110          ccc                   语文                   0
    11          ccc                   数学                   012          eee                   语文                   1
    13          eee                   数学                   1
    14          eee                   英文                   1
    aaa    数学    英文      语文        化学     物理 
    0       0       0        1      0 
    bbb    英文     语文     数学     物理
            0        0      1        1
    ccc    语文    数学
            0        0
    eee    语文     数学     英文
            1       1        1
      

  2.   


    create table u_table
    ([id] int, [name] nvarchar(20) , [function] nvarchar(20),   [ction] bit )
    insert into u_table select 1,'aaa','数学',0
    union all select 2,'aaa','英文',0
    union all select 3,'aaa','语文',0
    union all select 4,'aaa','化学',1
    union all select 5,'aaa','物理',0
    union all select 6,'bbb','英文',0
    union all select 7,'bbb','语文',0
    union all select 8,'bbb','数学',1
    union all select 9,'bbb','物理',1
    union all select 10,'ccc','语文',0
    union all select 11,'ccc','数学',0
    union all select 12,'eee','语文',1
    union all select 13,'eee','数学',1
    union all select 14,'eee','英文',1select * from u_table
    1 aaa 数学 0
    2 aaa 英文 0
    3 aaa 语文 0
    4 aaa 化学 1
    5 aaa 物理 0
    6 bbb 英文 0
    7 bbb 语文 0
    8 bbb 数学 1
    9 bbb 物理 1
    10 ccc 语文 0
    11 ccc 数学 0
    12 eee 语文 1
    13 eee 数学 1
    14 eee 英文 1
      

  3.   


    create proc up_RowTransferColumn
    asdeclare @sql nvarchar(4000)set @sql='Select [name] 'Select @sql=@sql+',max(case when [function]='''+[function]+ ''' then [ction] else -1 end) As '''+[function]+''''From (select [function] from u_table group by [function]) tset @sql=@sql+' From u_table Group By [name]'execute sp_executesql @sqlexec  up_RowTransferColumnaaa 1 0 0 0 0
    bbb -1 1 1 0 0
    ccc -1 0 -1 -1 0
    eee -1 1 -1 1 1
      

  4.   


    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="RowTransferColumn.aspx.cs" 
    Inherits="RowTransferColumn" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 
    Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <table>
        <asp:Repeater ID="rp" runat="server" OnItemDataBound="rp_ItemDataBound">
        <AlternatingItemTemplate>
        <asp:Literal ID="lit" runat="server"></asp:Literal>
        </AlternatingItemTemplate>
        <ItemTemplate>
        <asp:Literal ID="lit" runat="server"></asp:Literal>    
        </ItemTemplate>
        </asp:Repeater>
        </table>
        </form>
    </body>
    </html>using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    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.Data.SqlClient;public partial class RowTransferColumn : System.Web.UI.Page
    {
        protected string[] columnName;
        protected void Page_Load(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection("data source=.;uid=sa;pwd=sa;database=test"))
            {
                try
                {                                               
                    SqlDataAdapter da = new SqlDataAdapter("up_RowTransferColumn", con);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    DataSet ds = new DataSet();
                    da.Fill(ds);                columnName = new string[ds.Tables[0].Columns.Count-1];
                    for (int i = 1; i < ds.Tables[0].Columns.Count; i++)
                    {
                        columnName[i - 1] = ds.Tables[0].Columns[i].ColumnName;
                    }                rp.DataSource = ds.Tables[0].DefaultView;
                    rp.DataBind();               
                }
                catch (Exception ee)
                {
                    //...
                    
                }
                finally
                {
                    
                }        }    }
        protected void rp_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                DataRowView drv = e.Item.DataItem as DataRowView;
                if (drv != null)
                {
                    Literal lit = e.Item.FindControl("lit") as Literal;
                    if (lit != null)
                    {
                        lit.Text = "<tr><td rowspan='2'>" + drv["name"].ToString() + "</td>";
                        for (int i = 0; i < columnName.Length; i++)
                        {
                            if (columnName[i] != null && drv[columnName[i]] != null)
                            {
                                lit.Text += drv[columnName[i]].ToString() == "-1" ? "" : "<td>" + columnName[i] + "</td>";
                            }
                        }
                        lit.Text += "</tr><tr>";
                        for (int i = 0; i < columnName.Length; i++)
                        {
                            if (columnName[i] != null && drv[columnName[i]] != null)
                            {
                                lit.Text += drv[columnName[i]].ToString() == "-1" ? "" : "<td>" + drv[columnName[i]].ToString() + "</td>";
                            }
                        }
                        lit.Text += "</tr>";
                    }
                }
            }
        }
    }