简体   繁体   中英

c# WPF - Add total row in a DataGrid

I have this DataGrid :

在此处输入图片说明

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

EDIT 1 This is the XAML code:

<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;
    }

}

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

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

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM