0

实际上我正在使用中继器控件来显示一些报告。

 <asp:Repeater id="cdcatalog" runat="server">
<HeaderTemplate>
<table border="1" width="500">
<tr>
<th>Cost Code</th>
<th>Total</th>
<th>Price</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Eval("Cost_Code")%> </td>
<td><%#Eval("Total")%> </td>
<td><%#Eval("Price")%> </td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater> 

下面是我的 sql 查询

ALTER Proc [dbo].[RP_ByCost_Code]
@Date1 datetime,
@Date2 datetime

as
select Cost_Code , Total , (Total*12) as Price from mtblLog_Book where     Vehicle_Booking_Date between @Date1 and @Date2 order BY Cost_Code

报告如下格式

在此处输入图像描述

看到有重复的项目来了。让ENE-Direct我想为每个 ENE-Direct 行只取一次,它应该为所有成本代码显示一次

4

1 回答 1

0

您可以使用嵌套Repeater的 LINQGroupBy方法来实现此目的。

我不确定您的 DataSource 以及您如何绑定cdcatalog中继器,因此在此示例中,我使用的是CatalogItems 列表。这是CatalogItem课程:

public class CatalogItem
{
    public string Cost_Code { get; set; } 
    public int Total { get; set; }
    public decimal Price { get; set; } 
}

您将需要一个页面级列表:

    List<CatalogItem> items;

基本上,您会将外部中继器绑定到按 . 分组的列表Cost_Code。然后,内部中继器将绑定到CatalogItems 的过滤列表。像这样:

    protected void Page_Load(object sender, EventArgs e)
    {
        items = new List<CatalogItem>();
        items.Add(new CatalogItem() { Cost_Code = "ENE-Direct", Total = 33, Price = 196 });
        items.Add(new CatalogItem() { Cost_Code = "ENE-Direct", Total = 8, Price = 96 });
        items.Add(new CatalogItem() { Cost_Code = "ENE-Direct", Total = 15, Price = 1260 });
        items.Add(new CatalogItem() { Cost_Code = "ENE-Direct", Total = 10, Price = 228 });
        items.Add(new CatalogItem() { Cost_Code = "ENE-Direct", Total = 125, Price = 60 });
        items.Add(new CatalogItem() { Cost_Code = "IND038301", Total = 10, Price = 258 });
        items.Add(new CatalogItem() { Cost_Code = "IND038302", Total = 20, Price = 358 });
        items.Add(new CatalogItem() { Cost_Code = "IND038303", Total = 30, Price = 458 });
        items.Add(new CatalogItem() { Cost_Code = "IND038304", Total = 40, Price = 558 });
        this.cdcatalog.DataSource = items.GroupBy(c => c.Cost_Code).Select(c => new CatalogItem() { Cost_Code = c.Key });
        this.cdcatalog.DataBind();
    }

    protected void cdcatalog_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater rptItems = (Repeater)e.Item.FindControl("rptItems");
            CatalogItem catalogGroup = (CatalogItem)e.Item.DataItem;
            rptItems.DataSource = items.Where(i => i.Cost_Code == catalogGroup.Cost_Code);
            rptItems.DataBind();

        }
    }

ascx 代码如下所示:

    <asp:Repeater ID="cdcatalog" runat="server" OnItemDataBound="cdcatalog_ItemDataBound">
        <HeaderTemplate>
            <table border="1" width="500">
                <tr>
                    <th>Cost Code</th>
                    <th>Total</th>
                    <th>Price</th>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td><%#Eval("Cost_Code")%> </td>
            </tr>
            <asp:Repeater ID="rptItems" runat="server">
                <ItemTemplate>
                    <tr>
                        <td></td>
                        <td><%#Eval("Total")%> </td>
                        <td><%#Eval("Price")%> </td>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>

最终输出将如下所示:

在此处输入图像描述

于 2013-04-04T17:05:39.863 回答