.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> WPF >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

How to calculate totals of wpf datagrid column with dataset which is created by adding a DataTemplat

Posted By:Doudy       Posted Date: March 02, 2013    Points: 40    Category: WPF    URL: http://www.dotnetspark.com  

I want to show total amount of two columns
 

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:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    x:Class="AgamistaStore.Resources.DataTemplatesClass"
                     xmlns:myClasses="clr-namespace:AgamistaStore.Classes"
                    >
    <myClasses:TablesStateConverter  x:Key="TablesStateConverter"/>
    <myClasses:OrderItemNotEditableStateConverter x:Key="NotEditableStateConverter"/>

<!-- 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>
</ResourceDictionary>
MainWindow:

<Border Grid.Column="1" CornerRadius="6,6,6,6" BorderThickness="1,1,1,1" BorderBrush="#FFBCC7BB" 
                        Margin="9,10,8,10" x:Name="borderDetails">
                    <Grid HorizontalAlignment="Left" Width="439" Margin="0,0,0,0" VerticalAlignment="Top" 
                      Height="397" Visibility="Visible">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="352*" />
                            <RowDefinition Height="45*" />
                        </Grid.RowDefinitions>
                        <!--bill details -->
                        <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,


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply

    Read also another Resources from the same Author

Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend