根据SQL查询在网格视图中添加列

时间:2012-09-30 11:33:56

标签: asp.net sql-server-2008 sql-server-2005 asp.net-4.0 asp.net-3.5

我正在使用此查询:

select EmployeeId, receptionist_name, date_of_sale,
  count(date_of_sale) 
from FrontOffice 
group By EmployeeId, receptionist_name, date_of_sale 
order by date_of_sale

用于填充网格视图。但是我可以绑定EmployeeIdreceptionist_namedate_of_sale,所以你能告诉我如何在网格视图中绑定第4列以及如何填充网格视图。

更新:以下是我正在使用的代码:

<Columns> 
    <asp:BoundField HeaderText="Date" DataField="date_of_sale" /> 
    <asp:BoundField HeaderText="Employe Id" DataField="EmployeeId" /> 
    <asp:BoundField HeaderText="Receiptist Name" DataField="receptionist_name" /> 
    <asp:TemplateField HeaderText="No. of Prospectus Sale"> 
    <ItemTemplate> 
        <asp:Label ID="salecount" runat="server" 
          Text='<%# Eval("SaleCount") %>'></asp:Label> 
    </ItemTemplate> 
    </asp:TemplateField> 
</Columns>

2 个答案:

答案 0 :(得分:2)

执行以下操作:

  1. 将您的查询更改为:

    select EmployeeId, receptionist_name, date_of_sale, count(date_of_sale) as Total from FrontOffice group By EmployeeId, receptionist_name, date_of_sale order by date_of_sale

  2. 将Gridview代码更改为:

    Replace Eval("SaleCount") to Eval("Total")

答案 1 :(得分:1)

而不是更改整个代码只需将列别名为count(date_of_sale) As SaleCount

<Colums>
    <asp:BoundField HeaderText="Date" DataField="date_of_sale" />
    <asp:BoundField HeaderText="Employe Id" DataField="EmployeeId" /> 
    <asp:BoundField HeaderText="Receiptist Name" DataField="receptionist_name" /> 
    <asp:BoundField HeaderText="No. of Prospectus Sale" DataField="SaleCount" />
</Columns>