开发者

ListView Running total

I have a listview and would like to calculate a running total of values. every dataitem that I have bound to my listview, has an "amount". on ListView1_ItemDataBound, I would like to get the data item's "amount开发者_运维技巧" value, but I cannot find how to access the item.

This is kind of similar, i think, except I want a running total, not a total at the end.

Displaying totals in the ListView LayoutTemplate


You should be able to find the item in your DataItem inside your ListViewItem. Some sample code might look like:

public int ListViewTotal
{
 get; set;
}

  protected void MyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
  {
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
      // Retrieve the underlying data item. In this example
      // the underlying data item is a DataRowView object.        
      DataRowView rowView = (DataRowView)dataItem.DataItem;

      ListViewTotal += rowView["amount"];

      //Set text of listview control to new total:
      Label lbl = (Label)e.Item.FindControl("myAmountLabel");
      lbl.Text = ListViewTotal.ToString();
    }
  }

Hope that helps point you in the right direction.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜