bunglestink
bunglestink

Reputation: 778

Data Binding of Computed Properties in Entity Framework

I am trying to find an effective way to databind computed fields of an Entity Framework model in winforms. I am using Visual Studio 2008 and .NET framework 3.5. An example would be for EntityX, say I have columnA and columnB, and I want a computed property named sumAB. The way I have been doing this has been with partial classes:

public partial class EntityX 
{
    public int sumAB 
    {
        get 
        {
            return this.columnA + this.columnB;
        }
    }
}

I then am using visual studio to add EntityX as a data source, so I can use the automatic drag and drop data binding. The issue that I am having is that the computed properties are not appearing under the Data Source fields. I am looking for a way to have the automatic databinding of computed fields.

I know I could do this manually, but then I would have to also manually write all of the binding code to refresh the field when columnA or columnB are changed. I also do not have access to make this field computed on the SQL server side either.

Does anyone know of any ways to accomplish this or any other similar directions to persue?

Thanks!

UPDATE

I tested this on another machine, using Visual Studio 2010 and .NET 4, and I am still receiving the same behavior. Interestingly, I can manually create a textbox and add a databinding, and it will work fine, as below:

sumABTextBox.DataBindings.Add(
  new System.Windows.Forms.Binding("Text", EntityXBindingSource, "sumAB", true));

It also worked if I placed the property in the .edmx file, but this is not desirable, as it will be wiped any time the database is updated.

Any other ideas?

UPDATE 2

Still no solution... I had to use alternatives in my code to meet deadlines. I am still very interested in this if anyone finds an answer...

Upvotes: 3

Views: 2108

Answers (2)

Chad
Chad

Reputation: 3237

I basically just asked the same question (WPF not WinForms) yesterday - Show Computed Property in Entity Framework and WPF UI .

In short, I couldn't find any automatic support. I had to create the columns in my DataGridView for each computed property. Then, to let the DataGridView know that those properties have updated, I have to call OnPropertyChanged("ComputedPropertyName") in the OnChanged partial method.

e.g.

public partial class Test
{
    public int AB
    {
        get { return A * B; }
    }

    public partial void OnAChanged()
    {
        OnPropertyChanged("AB");
    }
    public partial void OnBChanged()
    {
        OnPropertyChanged("AB");
    }
}

If there is an automatic way for this to work, I would like to know too.

Upvotes: 1

Youp Bernoulli
Youp Bernoulli

Reputation: 5654

Sounds strange to me. Because I use the same approach in my project (Entity Framework, Self Tracking Entities, WCF, Winforms).

I constructed a simple winforms applciation to test it and the "computed" property just shows up in the databindings.Text property of a textbox control.

I created a simple class:

public partial class Test
{
    public int A { get; set; }
    public int B { get; set; }

    public Test()
    {
    }
}
public partial class Test
{
    public int AB
    {
        get { return A * B; }
    }
}

Added a bindingsource to a form (with datasource pointing to an object of type Test) and three textboxes binded to the bindingsource and the properties: A, B and AB. In the constructor of the form I created an instance of test, populated values for A and B, set bindingsource.DataSource to the created instance and thinks were groovy on the form ;)

This simple test case would be no different from your situation, other than that you work with entities which are off-course just Objects. I really can't see the problem from your description...Any further comments could help me help you further...

Upvotes: 1

Related Questions