开发者

Entity Datagrid : best way to add computed values?

I have a WPF datagrid databound to a collectionviewsource, which is itself based of a entity query

In my grid for example I have 3 columns Number1, Number2, Number3, all properties of an entity. If I want to add a column called SumNumber which is equal to Number1+Number2+Number3, what is the best way to do that so that when I change Number1, the value in SumNumber is updated ?

Thanks in advance


I'm almost there but I'm getting another error. See below a snippet of code

public partial class MyEntity
{
    public double MyCustomizedProperty{get;set;}

    public MyEntity()
    {
        this.PropertyChanged += Entity_PropertyChanged;
    }

    void Entity_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        switch (e.PropertyName )
        {
            case "Date" :
                MyCustomizedProperty = DateTime.Now.Second;
                ReportPropertyChanged("MyCustomizedProperty");
                break;
        }
    }
}

That compiles and all, but when I change "Date" I get a runtime error :

The property 'SigmaFinal' does 开发者_如何学Pythonnot have a valid entity mapping on the entity object. For more information, see the Entity Framework documentation.

I suppose this is due to the fact that the property is not in the OnStateManager. Can you please let me know how to fix that ?

Thanks


I would either create a Converter that would take all 3 values and return the sum of them, or I would expand the Entity Framework class to include an extra property

Here's an example of expanding the EF class to include the extra property:

public partial class MyEntity
{

    public MyEntity()
    {
        // Hook up PropertyChanged event to alert the UI
        // to update the Sum when any of the Values change
        this.PropertyChanged += MyEntity_PropertyChanged;
    }

    void MyEntity_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        switch e.PropertyName
        {
            case "Value1":
            case "Value2":
            case "Value3":
                ReportPropertyChanged("SumValues");
                break;
        }
    }


    public int SumValues
    {
        get
        {
            return Value1 + Value2 + Value3;
        }
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜