Search code examples
asp.netdynamic-data

Use a Computed Property as DisplayColumn in ASP.Net Dynamic Data?


I am trying to display a 'Software Release' table in an asp.net dynamic data site. The Release table has a build number stored as three int fields (Major, Minor, Build). I'm using EntityFramework, so I have an EF model bound to my database schema. In my dynamic data site, I want the build number to show up as a single field (Major.Minor.Build) wherever the Release object is shown (particularly when it shows as a foreign key on pages for related objects). Since this 'computed column' is not a field in my database, there doesn't seem to be anyway to get the Dynamic-Data to recognize or display it. I can add a property to the Release object (since it is a partial class generated by EF), but Dynamic-Data won't recognize it, because it isn't a 'column'. I want to edit the Release as three separate fields (major, minor, build), but when it is displayed, I want it to show as a single field. The DynamicData framework doesn't seem to support composite fields, and it won't display/bind to properties on the object if they aren't in the EF model. How do I make the formatted version number property the default display value?


Solution

  • I'm not sure if you are binding to a DataTable/DataSet returned from the database, or if you are binding to a Release object itself. Nor which kind of control.

    If you are binding to a DataSet/DataTable, simply change your SQL to return the version as one field:

    SELECT table1.Major + '.' + table1.Minor + '.' + table1.Build AS Version ....
    

    However, if you are binding to an object to, say, a DropDownList, I think that if you override the ToString method, it will become the Display value in the DropDownList:

    Public Overrides Function ToString() As String
        Return _major.ToString & '.' & _minor.ToString & '.' & _build.ToString
    End Sub