How to calculate the screen child datagrid totals in Microsoft Lightswitch?

There are two ways to calculate the screen child datagrid totals.

  1. Directly access the workspace data from the screen child datagrid.
  2. Use the existing data in screen child datagrid.

Now which method of calculating the totals in the screen child datagrid would you choose? My answer is, it depends on the requirement of the client.

To directly access the workspace data from the screen child datagrid you need to follow the three steps.

  1. Create a workspace object
  2. Get the query of the child datagrid
  3. Loop through the query to compute for the total

Here is the sample code snippet:

Dim ws = Me.Application.CreateDataWorkspace()
Dim rs= ws.ApplicationData.MyDataGrid.GetQuery().Execute()
Dim records= From r In rs

For Each record In records
    Total = Total + record.Amount
Next

To use the existing data in the screen child datagrid you need to follow the following steps:

1. Remove the paging of the data in the datagrid

2. Loop through the datagrid data to compute for the total

Thats it, the problem in method no. 2 is that you need to remove the paging support before you could loop through the data otherwise you can only calculate the data of the current page.

Below is an actual application example that uses method no. 2 in calculating the screen child data grid, of course I already added the screen data so that the result can be displayed in the screen.

Advertisements

2 thoughts on “How to calculate the screen child datagrid totals in Microsoft Lightswitch?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s