How to calculate totals of wpf datagrid columns which is filled by user data entry?
Posted by
Doudy under
WPF on 3/2/2013 12:36:01 PM |
Points: 75 | Views : 5175 | Status :
[Member]
I am trying to figure out what is the best way to calculate a total value of DataGrid column in wpf.
I´m using dataset to display data from SQL server in my Resources(DataTemplate) & bind it to datagrid like below:DataTemplate:
<!-- ProductName column -->
<DataTemplate x:Key="NotEditableStateColumnTemplate">
<Image Height="20" Width="20" Source="{Binding Path=NotEditable, Converter={StaticResource NotEditableStateConverter}, Mode=Default}" HorizontalAlignment="Stretch"/>
</DataTemplate>
<!-- Barcode column -->
<DataTemplate x:Key="ProductBarcodeColumnTemplate">
<TextBlock FontFamily="Tahoma" FontSize="14" Text="{Binding Path=ProductBarcode}" HorizontalAlignment="Stretch" Foreground="#FF44544A" />
</DataTemplate>
<!-- ProductName column -->
<DataTemplate x:Key="ProdNameColumnTemplate">
<TextBlock FontFamily="Tahoma" FontSize="14" Text="{Binding Path=ProductName}" HorizontalAlignment="Stretch" Foreground="#FF44544A"/>
</DataTemplate>
<!-- Price column -->
<DataTemplate x:Key="PriceNameColumnTemplate">
<TextBlock x:Name="SalePrice" FontFamily="Tahoma" FontSize="14" HorizontalAlignment="Stretch" Text="{Binding Path=SalePrice, StringFormat={}{0:#.##}}" Foreground="#FF44544A"/>
</DataTemplate>
<!-- Quntity column -->
<DataTemplate x:Key="QuntityColumnTemplate">
<TextBox x:Name="txtQuntity" Tag="{Binding}" PreviewTextInput="txtQuntity_PreviewTextInput" Style="{StaticResource TextBoxStyle}" Height="20" Foreground="#FF0A421F" FontFamily="Tahoma" FontSize="14" Text="{Binding Path=SoldQuantity}" HorizontalAlignment="Stretch"/>
</DataTemplate>
MainWindow:
<myClasses:SortableListView Margin="9,6,6,0" Height="auto" BorderBrush="{x:Null}" Background="{x:Null}" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" AllowDrop="True" VirtualizingStackPanel.IsVirtualizing="True" ScrollViewer.CanContentScroll="True" SelectionMode="Single" IsSynchronizedWithCurrentItem="True" ItemContainerStyle="{DynamicResource RestaurantEditListViewItemStyle}" Style="{DynamicResource RestaurantEditListViewStyle}" IsEnabled="True" HorizontalAlignment="Stretch" VerticalAlignment="Top" x:Name="gvOrderDetails" >
<myClasses:SortableListView.View>
<GridView AllowsColumnReorder="False" >
<GridViewColumn Width="4" HeaderContainerStyle="{DynamicResource FirstColumnGridViewColumnHeader}" CellTemplate="{StaticResource EmptyColumnTemplate}"/>
<GridViewColumn Width="8" CellTemplate="{StaticResource NotEditableStateColumnTemplate}" HeaderContainerStyle="{DynamicResource RestaurantDataGridViewColumnHeader}"/>
<myClasses:SortListViewColumn Header="??? ?????" SortProperty="ProductBarcode"
Width="80" CellTemplate="{StaticResource ProductBarcodeColumnTemplate}" SortStyle="RestaurantDataGridViewColumnHeader" HeaderContainerStyle="{DynamicResource RestaurantDataGridViewColumnHeader}" />
<myClasses:SortListViewColumn Header="???? ????????" SortProperty="ProductName"
Width="200" CellTemplate="{StaticResource ProdNameColumnTemplate}" SortStyle="RestaurantDataGridViewColumnHeader" HeaderContainerStyle="{DynamicResource RestaurantDataGridViewColumnHeader}" />
<myClasses:SortListViewColumn Header="?????" SortProperty="SalePrice" CellTemplate="{StaticResource PriceNameColumnTemplate}" Width="60" SortStyle="RestaurantDataGridViewColumnHeader" HeaderContainerStyle="{DynamicResource RestaurantDataGridViewColumnHeader}" />
<myClasses:SortListViewColumn Header="??????" SortProperty="UnitName" Width="50"
CellTemplate="{StaticResource QuntityColumnTemplate}" SortStyle="RestaurantDataGridViewColumnHeader" HeaderContainerStyle="{DynamicResource RestaurantDataGridViewColumnHeader}"/>
<GridViewColumn Width="4" CellTemplate="{StaticResource EmptyColumnTemplate}"
HeaderContainerStyle="{DynamicResource LastColumnGridViewColumnHeader}"/>
</GridView>
</myClasses:SortableListView.View>
</myClasses:SortableListView>Now I want to get the total sum (which is sum(SalePrice*SoldQuantity) to display in textbox, And SoldQuantity value can changed by user
for example:
Barcode ProductName Price Quantity
S1P3C34 T-shirt 150 1
The Total Amount which will appear in textbox must be (150*1) 150
If the user change the value of quantity to 3 , I want Total Amount value changed automatically to be (150*3) 450
What is the best practice to do this ?
Thanks,