I have a datagrid and I want to sum up all the values in a column. I think, to do this I need to sum the values in that column row by row. But I cant access rows because Datagrid.Rows code in c# does not work in WPF. I access total items count with:
datagrid.Items.Count;
How can I do datagrid colums total sum in WPF?
Datagrid xaml code:
<DataGrid BorderThickness="0" Name="grid_lab" RowHeight="25" IsReadOnly="True" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeColumns="True" CanUserResizeRows="False" AutoGenerateColumns="False" ColumnWidth="*" HorizontalGridLinesBrush="#FFDCDBDB" VerticalGridLinesBrush="#FFDCDBDB" HeadersVisibility="Column" VerticalAlignment="Top" Background="{x:Null}" MouseLeftButtonUp="grid_lab_MouseLeftButtonUp">
Datagrid textcolumn code:
<DataGridTextColumn Binding="{Binding Path=tutar}" Header="Tutar" MaxWidth="50">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Padding" Value="5,0,0,0"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
Total sum code in usercontrol loaded:
decimal sum = 0m;
for (int i = 0; i < grid_lab.Items.Count - 1; i++)
{
sum += (decimal.Parse((grid_lab.Columns[7].GetCellContent(grid_lab.Items[i]) as TextBlock).Text));
}