views:

107

answers:

1

Hi,

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.

http://stackoverflow.com/questions/212048/displaying-totals-in-the-listview-layouttemplate#212308

+1  A: 

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.

womp
How are you getting dataItem? Where does "dataItem" come from?
Mike
Thanks! I got it working. ((ListViewDataItem)e.Item).DataItem;
Mike