2015-05-24 54 views
0

我需要一些幫助來總結小數點。總和列表查看小數點

我使用下面的代碼來顯示一個列表視圖,並希望在列表視圖的最後一行顯示銷售價格的總和。

foreach (Stock c in stock) 
{ 
    string[] subitems = new string[4]; 
    subitems[0] = c.ID.ToString(); 
    subitems[1] = c.ItemDescription; 
    subitems[2] = "£" + c.PurchasePrice; 
    subitems[3] = "£" + c.SalePrice; 

    ListViewItem listItem = new ListViewItem(subitems); 
    SalelistView.Items.Add(listItem); 

    ItemNumberTofFnd.Text = ""; 
} 

如何顯示總銷售價格?

我試過decimal T += c.SalePrice,但我很不確定,因爲我之前沒有做過這種事情。

+0

十進制T + = c.SalePrice肯定會計算總價。在開始循環之前,不要忘記將'T'的值重置爲0 – gypsyCoder

回答

2

像這樣的東西?

decimal total; 
foreach (Stock c in stock) 
{ 
    string[] subitems = new string[4]; 
    subitems[0] = c.ID.ToString(); 
    subitems[1] = c.ItemDescription; 
    subitems[2] = "£" + c.PurchasePrice; 
    subitems[3] = "£" + c.SalePrice; 

    total += c.SalePrice; 

    ListViewItem listItem = new ListViewItem(subitems); 
    SalelistView.Items.Add(listItem); 

    ItemNumberTofFnd.Text = ""; 
} 
SalelistView.Items.Add("Total " + total);