实际上我正在使用转发器控件来显示一些报告。
<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行只取一次它,它应该显示一次所有成本代码
答案 0 :(得分:0)
您可以使用嵌套的Repeater
和LINQ GroupBy
方法来实现此目的。
我不确定您的DataSource以及如何绑定cdcatalog
转发器,因此在此示例中我使用的是CatalogItem
列表。这是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
分组的列表。然后内部转发器将绑定到CatalogItem
s的筛选列表。像这样:
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>
最终输出将如下所示: