如何通过在dataGrid列(wpf)中添加所有价格来显示总计 [英] How to show a grand Total by adding all the prices in the dataGrid column (wpf)
问题描述
我有这个 datagrid ,当我单击添加"按钮时,它将显示以下数据.我想要的是在网格底部动态显示grand_total(该区域以黄色突出显示)我已经搜索了很多,但是我无法访问任何数据网格页脚或类似的内容,而且我在WPF中是非常新的
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
我的Xaml代码:
<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>
这就是我设置网格数据的方式:
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);
其中Test是课程:
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; }
}
因为我是WPF的新手,所以我需要一个简化的解决方案
Please i need a simplified solution for this because i am new in WPF
推荐答案
在用于访问datagrid的代码文件顶部添加以下using语句
Add the following using statement to top of the code file used to access datagrid
using System.Linq;
创建一个TextBlock来显示后面代码的更新总数.
Create a TextBlock to show updated total from code behind.
<TextBlock x:Name="GrandTotal" />
代码添加数据后,使用datagrid中的项目计算总数
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);
可以将TextBlock和DataGrid放置在网格中,然后将TextBlock放置在所需的位置.
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>
这篇关于如何通过在dataGrid列(wpf)中添加所有价格来显示总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!