开发者

Need to count column cell value of having same type

I have a repeater control in which i display record as below..

Name Type noOfItems Amount

Abc   A      2        100
cbc   B      1        200
xyz   A      1        100
ret   c      3         50
wes   B      1        200

In this I show 3 rows.. what I want is to count No. of Type with their total noofItems

Something like...

 Total no of items:  (3) A, (2) B, (1) C
 Total Amount:       (2) 100, (2) 200, (1) 50

Kindly tr开发者_如何学Goy to help me out..please..

Thank you,..


Bind the ItemDataBound event of the repeater and keep dictionaries with the values in there. When the footer is being attached, set the values wherever you need it.

In the page:

<asp:Repeater ... OnItemDataBound="rptRepeaterName_ItemDataBound" ...>

In the code behind:

protected void rptRepeaterName_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ItemType.Item || e.Item.ItemType == ItemType.AlternatingItem)
    {
         // logic to count occurences of A, B, C and amounts in each their own dictionary ex: typeDictionary[type] += 1; amountDictionary[amount] += 1;
    }
    else if (e.Item.ItemType = ItemType.Footer)
    {
        // display values stored in both dictionaries
    }
}


Here is one more idea, you call the amount using a function that also keep the totals, and you use them at the end of your repeater.

<asp:Repeater ID="myId" runat="server">
    <ItemTemplate>
        <%#GetTheAmount(Container.DataItem)%>
    </ItemTemplate>
</asp:Repeater>

Total Amount: <%=cTotalAmount%>
Total Lines: <%=cTotalLines%>

and on code behind

public int cTotalAmount = 0;
public int cTotalLines = 0;

public string GetTheAmount(object oItem)
{
    int cCurrentAmount = (int)DataBinder.Eval(oItem, "Amount");
    cTotalLines++;
    cTotalAmount += cCurrentAmount;

    return cCurrentAmount.ToString();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜