none
Custom readonly property not showing in data sources RRS feed

  • Question

  •  I have added a readonly property "CityZip" to my Linq-to-SQL entity class, so I can bind the CityZip property to a datagridview.

    This is what my code looks like:

     

    Code Snippet

    Partial Class Customer

    Public ReadOnly Property CityZip() As String

    Get

    Return City + " " + Zip

    End Get

    End Property

    End Class

     

     

    When I add Customer to the project's data sources, CityZip does not show up and therefore I can't use the property in a datagridview (or with any other databound control).

     

    BTW the property CityZip can be used within code.

     

    Any help would we appreciated.

     

    Yours

    Detlef

     

    Wednesday, June 11, 2008 5:11 PM

All replies

  • Is this using the LinqDataSource control?

     

    [)amien

     

    Wednesday, June 11, 2008 9:02 PM
    Moderator
  • Hi Damien,

     

    I've created the entity with the linq-to-sql class from vs2008 and added the additional property as shown in my first post.

     

    Then I wanted to add this entity in the "data source window" of my project. For this I use the "Add data source wizard" (I don't know the english term because I'm using a german version). There I choose "object" and then my entity. The entity is added to the "data source window" but it only shows the properties from the database but not my custom property.

     

    Do you have any ideas what's happening here?

     

    Yours

    Detlef

    Thursday, June 12, 2008 6:50 AM
  • Hi,

    I tried to repro your scenario. From what I understand, I have  a mapping added to my project and I add your partial class to my mapping

     

        Partial Class Customer

            Public ReadOnly Property CityZip() As String

                Get

                    Return City + " " + "abc" 'tweaked as I dont have zip

                End Get

            End Property

        End Class

     

    I rebuild my project so that the new property is compiled in customer object and then I refresh my datasource view.

    I can see the new CityZip property added to it, also when I drag-drop the object on form the datafrid generated has the column for CityZip.

     

    So here's what I have

    1. have you rebuild the project after adding the property,

    2. Have you tried refreshing the datasource view.

     

    If I have missed what you are doing or it is still not working on your end please let us know and we will try to get a repro again.

     

     

    ~Deepti
    Thursday, June 12, 2008 8:52 PM
  • Hi,

     

    thank you for your answer and effort to reproduce my error. But unfortunately it is still not working for me.

     

    I followed all your steps (rebuilding the project, refreshing the datasource view) but the new property is still not showing up.

     

    The crazy thing is: if I put the mapping into a seperate project and then include this "mapping project" in my running project the property shows up in the datasource view. Only if the mapping and the datasource are within the same project, it doesn't work.

     

    So, any suggestions?

     

    Yours

    Detlef

     

    Friday, June 13, 2008 7:56 AM
  • Hi everybody,

     

    Just wanted to say I have exactly the same problem as Detlef. I also use the german version, perhaps there is just an issue in the german version.

     

    I also tried with a non-readonly attribute, but it doesn't work too.

     

    Hope somebody can help us out!

     

    Cheers,

    tom

     

    Sunday, June 15, 2008 9:26 PM
  • I use English version, but it doesn't work for me, either. Sad
    Friday, July 4, 2008 1:51 PM
  • Hi there

     

    The metadata used by Visual Studio for the corresponding data source definition only gets pulled from the designer generated source file (have a look in obj\Debug\TempPE) and does not seem to go looking for partial implementations of the class across other source files.  I have no idea if this behaviour is a design necessity or simply a bug.  Hacking the data source’s XML definition makes no difference which provides a few hints about what might be going on.

     

    A workaround is to put your Link-to-SQL classes in a class library project that is part of your solution and then create object data sources from out of the library for each other project in the solution that consumes the data source.  This works a treat as the metadata, including that describing your custom properties, is then pulled from the library and not from some mystic intermediate place deep within the tummy of VS.

     

    I like to bulk up the functionality of my Link-to-SQL classes to produce a more comprehensive business object rather than just to use them as a lightweight access shim over the top of the database.  Packaging this up into a library is in itself is not a bad thing as I find separating these things out results in a cleaner architecture and makes functionality available to other projects without having to share source code.

     

    Good luck with this.

     

    Kind regards

     

    Carl

    Wednesday, August 6, 2008 12:11 AM
  • Sorry for the delay in replying, Is it working for you now?

    If not we will really like to see your repro. Can you send over a small repro of your application or file a bug on http://www.connect.microsoft.com/ with the repro.

     

    Saturday, August 9, 2008 12:45 AM
  • Hi,

     

    for me it works when I put the partial class in it's own class library (like Carl posted in his reply). So this is a workaround for me. But I'm still not sure whether this is by design or is it a bug.

     

    Yours

    Detlef

     

    Saturday, August 9, 2008 9:14 AM
  • Hi

     

    Carl's solution works well for me. Thanks a lot Carl, much appreciated. The only problem is that the columns in the DataGridView that are bound to the custom properties won't sort when the column headers are clicked. All of the other bound columns sort fine. Any thoughts on how to fix this?

     

    Sol

    Monday, November 24, 2008 1:15 AM
  • Hi

     

    I cheat here a bit and although at design time I bind my grid to a binding source that is connected directly an Object Data Source created for the LINQ-to-SQL class, I implement my own collection wrapper to contain instance data and use this for runtime binding, sorting and finding. 

     

    The collection class implements IBindingList and IRasieItemChangedEvents and implements storage using a simple typed array and implements sorting using generic methods and a call to Array.Sort<T>(T[] array, Comparison<T> comparison).  The sort method implementation deals with the sort direction. 

     

    I have found that this approach gives flexibility in terms of where the data comes from (e.g. an in-memory collection or from the database itself) and is efficient when offering the user filtered views of data, incremental searching, etc. 

     

    The constructor of the collection wrapper simply receives an array of the target type which can come from anywhere such as a LINQ-to-SQL query of the data context or a LINQ query run across an in-memory collection of the underlying type.  The wrapper class is lightweight and the actual coding required trivial.

     

    Sol, I appreciate that this isn't a fix for you but its a way of avoiding the issue!  It would be nice if these things just worked though :-)

     

     

    Kind regards

     

    Carl

    Monday, November 24, 2008 8:21 PM
  • Any word from MSFT on this issue?
    I have recently run into the same problem and it definitely appears to be a bug.
    There are some cases where I would rather not break out in separate DLLs....

    Thanks!
    Tuesday, January 13, 2009 7:11 PM
  • Hi,

     

    I have the same issue with VS201 and EF4. Any hint is greatly appreciated.

    Monday, October 18, 2010 1:45 PM