Reputation: 1320
I am trying to get the total amount of values in the column SellingPrice in my datagrid
. How would I go about counting the total amount in c# coding?
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ItemId}" Header="id" MinWidth="20" MaxWidth="100" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding ItemCode}" Header="Code" MinWidth="100" MaxWidth="120" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding ItemDescription}" Header="Description" MinWidth="280" MaxWidth="360" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding ItemSellingPrice}" Header="Selling Price" MinWidth="120" MaxWidth="120" IsReadOnly="True"/>
<DataGridTextColumn Header="QTY" MinWidth="120" MaxWidth="120" IsReadOnly="False"/>
</DataGrid.Columns>
If you need any more information on my code/classes that I use, please feel free to ask. I will give it as soon as I can :) Thank you.
EDIT I want to display the Total Amount in a label
Upvotes: 0
Views: 422
Reputation: 1661
In your View Model, create a read-only property:
public double SumOfSomething
{
get
{
return YourCollection.Sum(x => x.ItemSellingPrice);
}
}
You can then bind a label to this property:
<label Content="{Binding SumOfSomething}"/>
Upvotes: 4
Reputation: 1844
I assume you have a List of items and bind that to the ItemsSource
of your DataGrid
. Furthermore I assume you have a Label
named _label
. Then your c# looks like:
public class Item
{
public int ItemId { get; set; }
public int ItemCode { get; set; }
public string ItemDescription { get; set; }
public double ItemSellingPrice { get; set; }
public int QTY { get; set; }
}
public class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var items = new List<Item>();
_dataGrid.ItemsSource = items;
var totalAmount = items.Sum(i => i.ItemSellingPrice);
_label.Content= totalAmount;
}
}
Upvotes: 2