c# WPF - 在 DataGrid 中添加总计行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21931507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:57:52  来源:igfitidea点击:

c# WPF - Add total row in a DataGrid

c#.netwpfdatagridcaliburn.micro

提问by puti26

I have this DataGrid :

我有这个 DataGrid :

enter image description here

在此处输入图片说明

I would like to add at the bottom of DataGrid a Row that contain Name: Total and Price: the Sum of all price. And if is possible that this row will be frozen. Do you have any ideas? The simple project is in this link : here

我想在 DataGrid 的底部添加一行,其中包含 Name: Total 和 Price: the Sum of all price。如果可能的话,这一行将被冻结。你有什么想法?简单的项目在这个链接中:这里

EDIT 1This is the XAML code:

编辑 1这是 XAML 代码:

<DataGrid ItemsSource="{Binding Articles}" AutoGenerateColumns="False" HorizontalAlignment="Left" CanUserAddRows="False" IsReadOnly="True">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="Price" Binding="{Binding Price}" />               
        </DataGrid.Columns>
    </DataGrid>

and this is the ViewModel:

这是视图模型:

[Export(typeof(IShell))]
public class MainViewModel : Screen
{
    public System.ComponentModel.ICollectionView Articles { get; set; }

    public MainViewModel()
    {
        Articles = CollectionViewSource.GetDefaultView(GetArticles());
    }


    private List<Article> GetArticles()
    {
        List<Article> arts = new List<Article>();

        arts.Add(new Article { Name = "Name1", Price = 2.80 });
        arts.Add(new Article { Name = "Name2", Price = 1.25 });
        arts.Add(new Article { Name = "Name3", Price = 9.32 });
        arts.Add(new Article { Name = "Name4", Price = 1.31 });
        arts.Add(new Article { Name = "Name5", Price = 0.80});
        arts.Add(new Article { Name = "Name6", Price = 2.50});
        arts.Add(new Article { Name = "Name7", Price = 0.50 });

        return arts;
    }

}

回答by Yuvaraj

Instead of adding a last row to the datagrid, you can rather add a text block below your datagrid and bind it to a property which has the total price.

您可以在数据网格下方添加一个文本块,并将其绑定到具有总价的属性,而不是向数据网格添加最后一行。

回答by user3336570

This should be helpful.i only added a new row with a method for making the sum

这应该有帮助。我只添加了一个新行,其中包含求和的方法

private List<Article> GetArticles()
    {
        List<Article> arts = new List<Article>();

        arts.Add(new Article { Name = "Name1", Price = 2.80 });
        arts.Add(new Article { Name = "Name2", Price = 1.25 });
        arts.Add(new Article { Name = "Name3", Price = 9.32 });
        arts.Add(new Article { Name = "Name4", Price = 1.31 });
        arts.Add(new Article { Name = "Name5", Price = 0.80 });
        arts.Add(new Article { Name = "Name6", Price = 2.50 });
        arts.Add(new Article { Name = "Name7", Price = 0.50 });
        arts.Add(new Article {Name = "Total", Price = GetTotal(arts)});
        return arts;
    }

    private double GetTotal(List<Article> arts)
    {
        double Total = 0;
        foreach (Article art in arts)
        {
            Total += double.Parse(art.Price.ToString());
        }
        return Total;
    }

Regards

问候

回答by PakKkO

one more:

多一个:

private List<Article> GetArticles()
    {
        List<Article> arts = new List<Article>();

        arts.Add(new Article { Name = "Name1", Price = 2.80 });
        arts.Add(new Article { Name = "Name2", Price = 1.25 });
        arts.Add(new Article { Name = "Name3", Price = 9.32 });
        arts.Add(new Article { Name = "Name4", Price = 1.31 });
        arts.Add(new Article { Name = "Name5", Price = 0.80 });
        arts.Add(new Article { Name = "Name6", Price = 2.50 });
        arts.Add(new Article { Name = "Name7", Price = 0.50 });
        arts.Add(new Article { Name = "Total", Price = arts.Sum(l => l.Price)});
        return arts;
    }