问题:假设有张学生成绩表(tb)如下:
姓名 课程 分数
张三 语文 74
张三 数学 83
张三 物理 93
李四 语文 74
李四 数学 84
李四 物理 94
想变成(得到如下结果): 
姓名 语文 数学 物理 
---- ---- ---- ----
李四 74   84   94
张三 74   83   93然后gridview
<asp:GridView ID="gdvNews2" runat="server" AutoGenerateColumns="False" CellSpacing="1" CellPadding="0" Width="100%" border="0" CssClass="border"
                            OnRowDataBound="gdvNews_RowDataBound">
                           <Columns>
                            <asp:BoundField DataField="name" HeaderText="姓名">
                            <ItemStyle Width="10%" />
                            </asp:BoundField>
                            <asp:BoundField DataField="num" HeaderText="语文">
                            <ItemStyle Width="10%" />
                            </asp:BoundField>
                            <asp:BoundField DataField="num" HeaderText="数学">
                            <ItemStyle Width="10%" />
                            </asp:BoundField>
                             ...
                            <asp:BoundField DataField="num" HeaderText="分数">
                            <ItemStyle Width="10%" />
                            </asp:BoundField>
                            </Columns>
                            <RowStyle CssClass="tdbg" Height="22px" />
                            <HeaderStyle CssClass="title" />
                        </asp:GridView>如果课程设为可管理员添加  怎么样绑定 能课程添加以后 数据自动绑定

解决方案 »

  1.   

    查询的时候 改下查询语句就行了  
    select 姓名=[name],
    语文=isnull(min(case when subject='语文' then score end),0),
    数学=isnull(min(case when subject='数学' then score end),0),
    英语=isnull(min(case when subject='英语' then score end),0),
    from scores group by [name]
    这样查询出来的结果绑定到gridview就行了
      

  2.   

    关键是 如果添加课程后 不用修改  绑定的会自动增加一列
    比如 增加课程 “科学”  怎么才能自动的多一行
    SQL语句我用动态语句可以用了 问题是怎么绑定
      

  3.   

    foreach (课程类 model in 课程集合)
    {
       BoundField newFiled = new BoundField();
       newFiled.DataField = model.Name;
       newFiled.HeaderText = model.Name;
       newFiled.ItemStyle.CssClass = 样式;
       GridView.Columns.Add(newFiled);
    }类似~