有一个datatable  dtStudent表如下:     StudentID   Name  Class    Score      1          张三    1班     500
      2          李四    1班     600
      3          王五    1班     650
      4          赵六    1班     600
我要用datagrid绑定,表格各个字段位置显示如下     StudentID   Name    1班   
---------------------------------      1          张三    500     
      2          李四    600     
      3          王五    650
      4          赵六    600
我帮定的时候直接将 “班级“ 分两栏绑定到 <HeaderTemplate>中, 因为class字段的值全部="1班", 以为可以在header显示如表头的"1班"一次,
<asp:TemplateColumn HeaderText="班级"
<ItemStyle BackColor="LightGoldenrodYellow"></ItemStyle>
<HeaderTemplate>
<asp:lable id="Class" runat="server"><%#DataBinder.Eval(Container,"DataItem.Class")%></asp:lable>
</HeaderTemplate>
<ItemTemplate>
<asp:Label id="Score" runat="server" Width="43px">
<%#DataBinder.Eval(Container,"DataItem.Score")%>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>

但,结果什么都不显示, <itemTemplate>能够正常工作显示完4条记录。
请问如果要把class的值“1班”,作为表头显示,应该如何绑定?

解决方案 »

  1.   

    the DataItem passed to the header template is null, so the binding on the headertemplate is meaningless, remove the headertemplate, and do
    DataTable dt = .....
    DataGrid1.Columns[0].HeaderText = dt.Rows[0]["Class"].ToString();
    DataGrid1.DataSource = dt.DefaultView;
    DataGrid1.DataBind();
    ....
      

  2.   

    <Columns>
    <asp:BoundColumn DataField="??" HeaderText="1班"></asp:BoundColumn>
    </Columns>这样不就行了?
      

  3.   

    DataTable dt = .....
    DataGrid1.Columns[0].HeaderText = dt.Rows[0]["Class"].ToString();
    DataGrid1.DataSource = dt.DefaultView;
    DataGrid1.DataBind();
    这个不错!