473,396 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to calculate totals of wpf datagrid column with dataset which is created by addin

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:
Expand|Select|Wrap|Line Numbers
  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  3.                     x:Class="AgamistaStore.Resources.DataTemplatesClass"
  4.                      xmlns:myClasses="clr-namespace:AgamistaStore.Classes"
  5.                     >
  6.     <myClasses:TablesStateConverter  x:Key="TablesStateConverter"/>
  7.     <myClasses:OrderItemNotEditableStateConverter x:Key="NotEditableStateConverter"/>
  8.  
  9. <!-- ProductName column -->
  10.     <DataTemplate x:Key="NotEditableStateColumnTemplate">
  11.         <Image Height="20" Width="20" Source="{Binding Path=NotEditable, 
  12.                 Converter={StaticResource NotEditableStateConverter}, Mode=Default}" HorizontalAlignment="Stretch"/>
  13.     </DataTemplate>
  14.     <!-- Barcode column -->
  15.     <DataTemplate x:Key="ProductBarcodeColumnTemplate">
  16.         <TextBlock  FontFamily="Tahoma" FontSize="14" Text="{Binding Path=ProductBarcode}" HorizontalAlignment="Stretch"
  17.                     Foreground="#FF44544A" />
  18.     </DataTemplate>
  19.     <!-- ProductName column -->
  20.     <DataTemplate x:Key="ProdNameColumnTemplate">
  21.         <TextBlock  FontFamily="Tahoma" FontSize="14" Text="{Binding Path=ProductName}" HorizontalAlignment="Stretch"
  22.                     Foreground="#FF44544A"/>
  23.     </DataTemplate>
  24.     <!-- Price column -->
  25.     <DataTemplate x:Key="PriceNameColumnTemplate">
  26.         <TextBlock x:Name="SalePrice" FontFamily="Tahoma" FontSize="14" HorizontalAlignment="Stretch" 
  27.                    Text="{Binding Path=SalePrice, StringFormat={}{0:#.##}}" Foreground="#FF44544A"/>
  28.     </DataTemplate>
  29.     <!-- Quntity column -->
  30.     <DataTemplate x:Key="QuntityColumnTemplate">
  31.         <TextBox x:Name="txtQuntity" Tag="{Binding}" PreviewTextInput="txtQuntity_PreviewTextInput" 
  32.                       Style="{StaticResource TextBoxStyle}" Height="20"  Foreground="#FF0A421F" 
  33.                      FontFamily="Tahoma" FontSize="14"  Text="{Binding Path=SoldQuantity}" HorizontalAlignment="Stretch"/>
  34.     </DataTemplate>
  35. </ResourceDictionary>
MainWindow:
Expand|Select|Wrap|Line Numbers
  1. <Border Grid.Column="1" CornerRadius="6,6,6,6" BorderThickness="1,1,1,1" BorderBrush="#FFBCC7BB" 
  2.                         Margin="9,10,8,10" x:Name="borderDetails">
  3.                     <Grid HorizontalAlignment="Left" Width="439" Margin="0,0,0,0" VerticalAlignment="Top" 
  4.                       Height="397" Visibility="Visible">
  5.                         <Grid.RowDefinitions>
  6.                             <RowDefinition Height="352*" />
  7.                             <RowDefinition Height="45*" />
  8.                         </Grid.RowDefinitions>
  9.                         <!--bill details -->
  10.                         <myClasses:SortableListView Margin="9,6,6,0" Height="auto" BorderBrush="{x:Null}"
  11.                                   Background="{x:Null}"   ScrollViewer.VerticalScrollBarVisibility="Auto"
  12.                                   ScrollViewer.HorizontalScrollBarVisibility="Auto" AllowDrop="True" 
  13.                                   VirtualizingStackPanel.IsVirtualizing="True" ScrollViewer.CanContentScroll="True"
  14.                                   SelectionMode="Single"  IsSynchronizedWithCurrentItem="True"
  15.                                   ItemContainerStyle="{DynamicResource RestaurantEditListViewItemStyle}"
  16.                                   Style="{DynamicResource RestaurantEditListViewStyle}" IsEnabled="True" 
  17.                                   HorizontalAlignment="Stretch" VerticalAlignment="Top" x:Name="gvOrderDetails" >
  18.                             <myClasses:SortableListView.View>
  19.                                 <GridView AllowsColumnReorder="False" >
  20.                                     <GridViewColumn Width="4" 
  21.                                            HeaderContainerStyle="{DynamicResource FirstColumnGridViewColumnHeader}" 
  22.                                            CellTemplate="{StaticResource EmptyColumnTemplate}"/>
  23.                                     <GridViewColumn Width="8" CellTemplate="{StaticResource NotEditableStateColumnTemplate}" 
  24.                                         HeaderContainerStyle="{DynamicResource RestaurantDataGridViewColumnHeader}"/>
  25.                                     <myClasses:SortListViewColumn Header="??? ?????" SortProperty="ProductBarcode" 
  26.                                          Width="80" CellTemplate="{StaticResource ProductBarcodeColumnTemplate}"  
  27.                                          SortStyle="RestaurantDataGridViewColumnHeader" 
  28.                                          HeaderContainerStyle="{DynamicResource RestaurantDataGridViewColumnHeader}" />
  29.                                     <myClasses:SortListViewColumn Header="???? ????????" SortProperty="ProductName" 
  30.                                          Width="200" CellTemplate="{StaticResource ProdNameColumnTemplate}"  
  31.                                          SortStyle="RestaurantDataGridViewColumnHeader" 
  32.                                          HeaderContainerStyle="{DynamicResource RestaurantDataGridViewColumnHeader}" />
  33.                                     <myClasses:SortListViewColumn Header="?????"  SortProperty="SalePrice"
  34.                                          CellTemplate="{StaticResource PriceNameColumnTemplate}" 
  35.                                          Width="60" SortStyle="RestaurantDataGridViewColumnHeader" 
  36.                                          HeaderContainerStyle="{DynamicResource RestaurantDataGridViewColumnHeader}" />
  37.                                     <myClasses:SortListViewColumn Header="??????"  SortProperty="UnitName" Width="50" 
  38.                                          CellTemplate="{StaticResource QuntityColumnTemplate}" 
  39.                                          SortStyle="RestaurantDataGridViewColumnHeader" 
  40.                                          HeaderContainerStyle="{DynamicResource RestaurantDataGridViewColumnHeader}"/>
  41.                                     <GridViewColumn Width="4" CellTemplate="{StaticResource EmptyColumnTemplate}" 
  42.                                          HeaderContainerStyle="{DynamicResource LastColumnGridViewColumnHeader}"/>
  43.                                 </GridView>
  44.                             </myClasses:SortableListView.View>
  45.                         </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,
Feb 27 '13 #1
1 5024
M1kkelZU
80 64KB
If I understand this correctly, you want a calculation done everytime the user changed the quantity of what product he/she wants to buy right?

Best way is to make an integer and use the sum you put SalePrice*SoldQuantity.

like
Expand|Select|Wrap|Line Numbers
  1. int soldAmount;
  2. soldAmount = SalePrice * SoldQuantity;
  3.  
  4. textBox1.Text = soldAmount.Convert.ToString();
I'm not 100% sure if that is the way but its something. Also not sure if I have the right jist of what you want either, maybe I'm right maybe not.
Mar 1 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Stephen | last post by:
Is it possible to add a tooltip in the column of a Datagrid. Can someone please show me how I would do this for the datagrid column below. Thanks for any help anyone can give me. Here is the...
4
by: Steve B. | last post by:
How do I find the column (index) number at runtime of a particular DataGrid column if I know the column header string //somthing like the following int x = datagrid.column;
3
by: McNutt Consulting | last post by:
I'm trying to implement a datagrid with dynamically created columns. The data is coming out of a very simple XML file, and it's bound through a dataview. The datagrid is just a shell definition...
1
by: Chris | last post by:
Is there a way to autosize the Datagrid column widths to just a bit larger than the data. I know how to manually size the column in code but I have a dynamically created datagrid and it work...
9
by: Roy | last post by:
Hey all, On my html page I have a datagrid with the column: <boundColumn datafield="xyz" visible = false> </boundColumn> In my code behind, within item data bound event, I dynamically set the...
1
by: ElenaR | last post by:
I need to figure out how to name my column headers in a DataGrid. In VB6, I could write DataGrid1.Columns(0).Caption = "ID". What is the format for VB.NET? Thanks in Advance!
5
by: DC Gringo | last post by:
I have a simple datagrid and need to output the value of "0" (zero) if the value is null. <asp:BoundColumn DataField="clnPopulationCensus" SortExpression="clnPopulationCensus"...
6
by: Aaron Smith | last post by:
Is there a way to put a limit on the text size of a datagrid column? Thanks, Aaron -- --- Aaron Smith Remove -1- to E-Mail me. Spam Sucks.
2
by: Starbuck | last post by:
Hi Is there anyway of capturing when the users adjusts the width of a datagrid column so the new column width can be stored. Thanks in advance
4
by: gane | last post by:
Hi, I am creating datagrid bound column dynamically and need to check if a datagrid column already exists?Is there a way to check this? thanks gane
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.