Gaurav
Gaurav

Reputation: 597

hide the repeated string in repeater

actually I am using a repeater control for showing some reports.

 <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> 

below is my sql query

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

and the report comes like below format

enter image description here

see there are repeated items is coming. Let take ENE-Direct I want to take it just once for every ENE-Direct row and it should show once for all Cost Codes

Upvotes: 0

Views: 245

Answers (1)

Shai Cohen
Shai Cohen

Reputation: 6249

You can use a nested Repeater along with the LINQ GroupBy method to achieve this.

I am not sure of your DataSource and how you are binding the cdcatalog repeater, so in this example I am using a List of CatalogItems. This is the CatalogItem class:

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

You will need a page-level List:

    List<CatalogItem> items;

Basically, you will bind the outside repeater to a list that is grouped by Cost_Code. The inner repeater will then be bound to a filtered list of CatalogItems. Like so:

    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();

        }
    }

The ascx code will look like this:

    <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>

The final output will look like this:

enter image description here

Upvotes: 0

Related Questions