次の方法で共有


Getting the Most out of LightSwitch Summary Properties

Note: This article has been updated for Beta 2 on 3/17/2011

Summary Properties in LightSwitch are properties on your entities (tables) that “describe” them (kind of like the .ToString() method on objects in .NET). A summary property is used by LightSwitch to determine what to display when a row of data is represented on a screen. Therefore, it’s important to get them right so that your data is represented to users in a clear way. In this post I’ll explain how to specify these properties on entities as well as how they work in LightSwitch applications.

Specifying Summary Properties on Entities (Tables)

Regardless of whether you are creating new database tables or attaching to an existing data source, summary properties are used to describe your entities as you model them in the designer. By default, LightSwitch will choose the first string property on your entity as the summary property. For instance, say we have a Customer entity with the following properties pictured below. If you select the Customer entity itself (by clicking on the name of it) you will see that the summary property is automatically set to LastName in the properties window. This is because the LastName property is the first property listed as type string.

image

In most cases this is the correct behavior you will want, however, for a Customer it makes more sense to display their full name instead. We can easily do this using a computed property. To add one, click on the “Computed Property” button at the top of the designer. This sets the Is Computed property in the properties window for you. Name the property FullName and then click on the “Edit Method” link in the properties window. This method is where you return the value of the computed field.

image

Computed fields are not stored in the underlying database, they are computed on the entity and only live in the data model. For FullName, I will return “LastName, FirstName” by writing the following code (in bold).

 Public Class Customer

    Private Sub FullName_Compute(ByRef result As String)
        ' Set result to the desired field value
result = Me.LastName + ", " + Me.FirstName
    End Sub

End Class

To set the FullName as the summary property, go back and select the entity’s name and you will now see FullName in the dropdown on the properties window as an available choice to use as the entity’s summary property.

Summary Properties in Action

Summary properties are displayed anytime you use the Summary control on a screen or when LightSwitch generates a layout for you that needs to display a description of the entity. For instance, to see this summary property in action create a search screen for the customer. Click on the “Screen…” button at the top of the designer, select the Search Screen template and select Customer as the screen data:

image

(Note, if you don’t have any records in your development database then also add a New Data Screen to enter some data first).Take a look at the Full Name label control in the screen designer of the search screen. Because this is a summary property it will display as a hyperlink to allow the user to open the specific record as indicated by the “Show as Link” in the Appearance section of the properties window. You can also choose which screen it should open. By default, the default edit screen is opened.

image

Hit F5 to run the application and open the customer search screen. Notice that the Full Name field is displaying as a clickable hyperlink. 

image

If we click it, then then LightSwitch will generate an edit screen for us to access the record. Notice that the titleof the generated Edit screen also displays the summary property:

image

Summary properties also show up in child grids and modal window pickers as well. 

More Examples of Summary Properties

Summary properties are not required to be strings, they just have to be able to be represented as strings. For instance, maybe you have an entity that captures date ranges or some item that would be better represented as a date. For our example say we have an Appointment entity and we’ve set a relation where a Customer can have many Appointments. The Appointment table has properties Subject, StartTime, EndTime and some Notes. We can select any of these properties as summary fields. You can even mix+match them by creating a computed property just like before, you just need to make sure the properties can be converted to a string.

 Private Sub Summary_Compute(ByRef result As String)
    ' Set result to the desired field value
    result = Me.StartTime + ": " + Me.Subject
End Sub

Another thing that you can do is show data from a related entity. For instance if you have an OrderDetail line item that has a reference lookup value to a Product table, you may want to display the Product Name on the OrderDetail. Create a computed property and then just walk the association to get the property you want on the related entity. Just make sure you perform a null check on the related property first:

 Private Sub Summary_Compute(ByRef result As String)
    ' Set result to the desired field value
    If Me.Product Is Nothing Then
        result = "<New Product> - Quantity: " + Me.Quantity
    Else
        result = Me.Product.Name + " - Quantity: " + Me.Quantity
    End If
End Sub

Keep in mind that summary properties are meant to be displayed throughout the application and should not be too lengthy or complicated. Keeping them down to 50 characters or less will make the application look less cluttered.

Enjoy!

Comments

  • Anonymous
    December 07, 2010
    Beth, What happens when someone changes LastName and/or FirstName? Does the Summary get updated (computed again) on the fly?

  • Anonymous
    March 17, 2011
    @Luciano, yes that's correct.

  • Anonymous
    January 19, 2012
    i tried to leave a comment but it didnt work

  • Anonymous
    March 29, 2012

  1. What about sorting on the computed property?
  2. What about search on the conputed property? Neither seem to work, nor can I find any examples.
  • Anonymous
    June 24, 2012
    Hi Beth, The computed property doesn't support the fuzzy search: search the full name directly. Is there any tips to handle this? Thanks!

  • Anonymous
    September 07, 2012
    Very cool. I'm using the Contacts Walk Thru and this was a link in that app so I thought I'd check it out.. Very good.. Beth, write a good lightswitch book.. Get MS to certify one in this app. I love it.. Tim

  • Anonymous
    June 01, 2013
    Hi Beth How can we do this in the HTML Client? It doesn't let you use a computed property as the summary property?

  • Anonymous
    June 17, 2013
    Beth, I came a little bit late to new LightSwitch new version. After upgrade, I cannot see "summary property" on the entity! Where did it go? Do I have to override ToString()?

  • Anonymous
    June 17, 2013
    OK, never mind, I found it! I just had to click on "Client" Perspective.

  • Anonymous
    December 04, 2013
    How can i do it with an register screen? if i have a field char(1) with W (week) or M (Month) values

  • Anonymous
    April 02, 2014
    Something weird is happening. I added a computed property in server perspective, then switch to client perspective and select my entity name and in the properties window I see summary property but I don't see the computed property that I added. I have saved, closed and opened the lsml file but I don't see it in the drop down.

  • Anonymous
    April 17, 2014
    I have the same problem that Mahesh has.

  • Anonymous
    April 17, 2014
    @Mahesh / @Guido -- Calculated properties only apply to the Server and Silverlight desktop client, not HTML client. This is because you need to write JavaScript on the HTML client. You can still set multiple columns to display on the HTML client screens though (i.e. to show a customers' full name for instance). If you need calculated properties on the HTML client see - lightswitchhelpwebsite.com/.../Computed-Properties-With-the-LightSwitch-HTML-Client.aspx and msdn.microsoft.com/.../jj733571.aspx Cheers, -Beth

  • Anonymous
    April 27, 2014
    Hi, thanks for the tutorial. I set the date format as "d" (without time). It works using a data grid but when I use the list to show the summary (date), the standard format with the time appears. How can I disable the time from the summary? Thanks in advance.

  • Anonymous
    August 15, 2014
    Is it possible to set the display as link property based on permissions/roles?

  • Anonymous
    October 19, 2014
    Hi Beth, Same I am trying in HTML Client side. I have to concatenate firstname and lastname inside table control in vs2013 lightswitch html client. I tried using custom control and then render/postrender but no luck. Basically in render/postrender method, it give contentItem, which hold only once entity field. but i require to field(firstname and last name) to concatenate. I tried string variable in the screen and render method but same problem. I have only contentItem. I tried calculate field also but it's not accessible in html screen side. Problem look simple but I am not getting any idea. Please assist asap.

  • Anonymous
    December 12, 2014
    Deniz - I have the same problem. Prior to the 2013 upgrade I used DOB.value.ToShortString() and it worked. Now when I create a new contact it throws a NullExceptionError. I removed the ,value.ToShortString() and it does not throw the error but displays the time in the date of birth even with the "d" format entered.

  • Anonymous
    December 12, 2014
    Deniz - I have the same issue. Prior to the 2013 upgrade I used DOB.Value.ToShortString() in my summary and it performed correctly giving m a date with no time. Since 2013 this causes a NullExceptionError when I create a new person. If I remove the .Value.ToShortString() I do not get the error but the time returns even with the "d" formatting.

  • Anonymous
    June 03, 2017
    When I originally commented I appear to have clicked the -Notify me when new comments are added- checkbox and from now on each time a comment is added I get 4 emails with the same comment. Is there a way you are able to remove me from that service? Many thanks!

  • Anonymous
    June 28, 2017
    " You have brought up a very good details , thanks for the post. "