Frage

I have this 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

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

}
War es hilfreich?

Lösung 2

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

Andere Tipps

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.

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top