本文介绍了WPF Datagrid列总计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据网格,我想对一列中的所有值求和。我认为,要做到这一点,我需要逐行求和该列中的值。但是我无法访问行,因为C#中的Datagrid.Rows代码在WPF中不起作用。我用以下项访问总项计数:

I have a datagrid and I want to sum up all the values in a column. I think, to do this I need to sum the values in that column row by row. But I cant access rows because Datagrid.Rows code in c# does not work in WPF. I access total items count with:

datagrid.Items.Count;

如何在WPF中处理datagrid列的总金额?

How can I do datagrid colums total sum in WPF?

Datagrid xaml代码:

Datagrid xaml code:

<DataGrid BorderThickness="0" Name="grid_lab" RowHeight="25" IsReadOnly="True"  CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeColumns="True" CanUserResizeRows="False" AutoGenerateColumns="False" ColumnWidth="*" HorizontalGridLinesBrush="#FFDCDBDB" VerticalGridLinesBrush="#FFDCDBDB" HeadersVisibility="Column" VerticalAlignment="Top" Background="{x:Null}" MouseLeftButtonUp="grid_lab_MouseLeftButtonUp">

Datagrid文本列代码:

Datagrid textcolumn code:

<DataGridTextColumn Binding="{Binding Path=tutar}" Header="Tutar" MaxWidth="50">
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock">
                                    <Setter Property="VerticalAlignment" Value="Center"/>
                                    <Setter Property="Padding" Value="5,0,0,0"/>
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>

已加载用户控件中的总和代码:

Total sum code in usercontrol loaded:

            decimal sum = 0m;
        for (int i = 0; i < grid_lab.Items.Count - 1; i++)
        {
            sum += (decimal.Parse((grid_lab.Columns[7].GetCellContent(grid_lab.Items[i]) as TextBlock).Text));
        }


推荐答案

以获取您所需要的列要求和,请使用 .Columns 集合属性与列的索引。然后,您可以在列上使用 GetCellContent()方法访问其单元格。请记住,单元格的内容是 TextBlock 类型的对象,因此将其强制转换为得到 Text 属性然后将值解析为十进制(或其他适合您的值的类型)。

To get the column that you want to sum, use the .Columns collection property with the index of the column. Then you can use the GetCellContent() method on the column to access its cell. Keep in mind that the content of the cell is an object of TextBlock type, so cast it, get its Text property and parse the value to decimal (or any other type suitable to your values).

想要第八列(索引= 7),可以尝试以下操作:

In your case, you want the eighth column (index = 7), you can try this:

decimal sum = 0m;
for (int i = 0; i < grid_lab.Items.Count - 1; i++) {
    sum += (decimal.Parse((grid_lab.Columns[7].GetCellContent(grid_lab.Items[i])as TextBlock).Text));
}

请注意,我们循环到 Items.Count-1 ,因为最后一项是占位符。

Notice that we loop to Items.Count - 1 because the last item is a placeholder.

或者,您可以使用 ItemsSource 属性,该属性存储绑定对象的集合,然后将每个项目强制转换为要绑定的类型。

Alternatively, you can use the ItemsSource property which stores the collection of bound objects, and you then cast each item to the type that you're binding to.

您要绑定到 DataTable ,您正在计算 tutar 列的总数。您可以使用下面的代码获取总计:

You're binding to DataTable and you're calculating the total for the tutar column. You can use the code below to get the total:

decimal sum = 0m;
foreach (DataRowView row in grid_lab.ItemsSource) {
    sum += (decimal)row["tutar"];
}

但是,由于您要在要获取的同一函数中计算总和数据库中的 DataTable 并绑定网格,为什么不直接从 DataTable 计算总和?您可以这样操作(与上面的代码非常相似,但是直接从 DataTable 而不是从网格):

However, since you're calculating the sum in the same function which is fetching the DataTable from the database and binding the grid, why don't you calculate the sum directly from the DataTable? You can do it like this (which is very similar to the code above, but directly from the DataTable instead of the grid):

decimal sum = 0;
foreach (DataRow row in dbdataset.Rows) {
    sum += Convert.ToDecimal(row["tutar"]);
}

注意:为什么要提供 DataTable 名称 dbdataset ?您应该考虑正确命名变量。

Note: Why are you giving your DataTable the name dbdataset? You should consider naming your variables correctly.

这篇关于WPF Datagrid列总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 04:51