Inloggen
 
 
 
 
    
Extend a linq class with use of partial class
Location: BlogsHenri Koppen - Developer    
Posted by: Henri Koppen 18-9-2008 13:42
Using Linq has a lot good features. But sometimes trivial tasks are cumbersome. Displaying extra fields of associated linq classes in a datagridview in a databinding scenario took me more then some hours to achive.
I had posted some topics on forums regarding the use of extra display fields in a Linq with databinding scenario.

Here's my situation. In the interface on a left panel I displayed a datagridview of persons. When clicking on a row I saw the details of that person on a form. Here I could change a person and save it. I was using a bindingsource and a linq query to fill the bindingsource. If you want to edit items in a binding source they have to be of one Linq to SQL class (in a .dbml file). A person is linked to an organisation, and to display the organisation name in de detail form is simple. Its something like Person.Organisation.Name. However: To display the organisation name in the DataGridView was not so trivial, using Person.Organisation.Name did not work.

First I used a partial class in the VB file behind the Linq DBML file to override the ToString property to display the Organisationname. But I got stuck when I wanted to display more than just the organisation name.

Using the Partial Class I made I added some extra properties in de Person class like OrganisationName.

this is what it looked like:
Code:

Partial Class Person

    Public ReadOnly Property OrganisationName() As String
        Get
            Return If(Me.Organisation Is Nothing, "", Me.Organisation.Name)
        End Get
    End Property
End Class



Property has to be public and ReadOnly. The class Person has to have an association with class Organisation.

In the properties of the datagridview I could fill the DataPropertyName with "OrganisationName".

This really worked for me.
Permalink |  Trackback
  
 
Weblogs
    
Archief
    
Zoeken
    
 
 
 
 
Copyright 2006-2009 by Arcencus
Privacy Statement | Terms Of Use