How to show a grand Total by adding all the prices

2019-09-08 04:27发布

I have this datagrid which shows the following data when i click on add button. what i want is to show the grand_total at the bottom of the grid Dynamically(the area is highlighted in in yellow color) i have search alot but i am unable to access any datagrid footer or things like that, also i am very new in WPF

Example

My Xaml CODE:

<DataGrid x:Name="dataGrid" CanUserAddRows="True" ItemsSource="{Binding ''}" AutoGenerateColumns="False" HorizontalAlignment="Left" VerticalAlignment="Top" Height="207" Width="507" IsReadOnly="True" Margin="0,66,0,0">
                <DataGrid.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FF23B4EA" Offset="0"/>
                        <GradientStop Color="#FF23D2EE" Offset="0.992"/>
                    </LinearGradientBrush>
                </DataGrid.Background>
                <DataGrid.Columns>
                    <DataGridTextColumn x:Name="product_name" Header="Product"  Binding="{Binding Title}" Width="*"/>
                    <DataGridTextColumn x:Name="unit_price" Header="Unit Price" Binding="{Binding Price}" Width="*"/>
                    <DataGridTextColumn x:Name="quantity" Header="Quantity" Binding="{Binding Quantity}" Width="*"/>
                    <DataGridTextColumn x:Name="Total"  Header="Total" Binding="{Binding Total_Price}" Width="*"/>

                    <DataGridTemplateColumn Width="*" Header="Operation">
                        <DataGridTemplateColumn.CellTemplate >
                            <DataTemplate >
                                <Button Content="Delete" x:Name="btnDelete"
                                    Click="btnDelete_Click"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>                   
                </DataGrid.Columns>                
          </DataGrid>

Thats how i am setting the grid data:

int q, p;
                q = int.Parse(QuantityTextbox.Text);
                p = int.Parse(PriceTextbox.Text);
                double T_price = q * p;
                var data = new Test
                {
                    Title = titleTextbox.Text,
                    Price = PriceTextbox.Text,
                    Quantity = QuantityTextbox.Text,
                    Total_Price = T_price.ToString()
               };
                dataGrid.Items.Add(data);

Where Test is the class:

 public class Test
        {
            public string Title { get; set; }
            public string Price { get; set; }
            public string Quantity { get; set; }
            public string Total_Price { get; set; }
            public string grand_total { get; set; }
        }

Please i need a simplified solution for this because i am new in WPF

1条回答
Juvenile、少年°
2楼-- · 2019-09-08 05:10

Add the following using statement to top of the code file used to access datagrid

using System.Linq;

Create a TextBlock to show updated total from code behind.

<TextBlock x:Name="GrandTotal" />

After your code adding data, calculate the total using items in datagrid

...
dataGrid.Items.Add(data);
var grandTotal = dataGrid.Items.Cast<Test>().Sum(x => int.Parse(x.Total_Price));
GrandTotal.Text = String.Format("{0:c}", grandTotal);

The TextBlock and DataGrid can be placed in a Grid and you can then position the TextBlock to where you need it to be.

<Grid>
    <DataGrid x:Name="dataGrid" CanUserAddRows="True" ItemsSource="{Binding ''}" AutoGenerateColumns="False" HorizontalAlignment="Left" VerticalAlignment="Top" Height="207" Width="507" IsReadOnly="True" Margin="0,66,0,0">
                <DataGrid.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FF23B4EA" Offset="0"/>
                        <GradientStop Color="#FF23D2EE" Offset="0.992"/>
                    </LinearGradientBrush>
                </DataGrid.Background>
                <DataGrid.Columns>
                    <DataGridTextColumn x:Name="product_name" Header="Product"  Binding="{Binding Title}" Width="*"/>
                    <DataGridTextColumn x:Name="unit_price" Header="Unit Price" Binding="{Binding Price}" Width="*"/>
                    <DataGridTextColumn x:Name="quantity" Header="Quantity" Binding="{Binding Quantity}" Width="*"/>
                    <DataGridTextColumn x:Name="Total"  Header="Total" Binding="{Binding Total_Price}" Width="*"/>
                    <DataGridTemplateColumn Width="*" Header="Operation">
                        <DataGridTemplateColumn.CellTemplate >
                            <DataTemplate >
                                <Button Content="Delete" x:Name="btnDelete"
                                    Click="btnDelete_Click"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>                   
                </DataGrid.Columns>                
        </DataGrid>
        <TextBlock x:Name="GrandTotal" HorizontalAlignment="Right" VerticalAlighment="Bottom" Margin="0,0,24,24" />
    </Grid>
查看更多
登录 后发表回答