Question

I'm trying to export ALL records from a Visual Collection to excel.

My code looks like:

Imports System.Runtime.InteropServices.Automation

Namespace LightSwitchApplication

    Public Class Assignacions_GR_a_DPT_o_IR_manuals

        Private Sub ExportacioExcel_Execute()
            Try
                ...

                For Each GR In Me.A_LIGHTSWITCH_QUERY
                    rowCount += 1
                    excel.Cells(rowCount, 1).Value = GR.ToString

But only currently A_LIGHTSWITCH_QUERY showed records in grid are exported. Is there a way to get all query records?

Code is wrote in Screen Command Bar Execute Code.

EDITED

About your point 1,3 in Yann answer:

Screen has 2 queries: MAIN_QUERY and LOOKUP_QUERY.

First one, MAIN_QUERY, is binded to a grid.

LOOKUP_QUERY is a query to lookup values for a MAIN_QUERY field (Choices on an AutoComplete BOX).

For each loop iterates over LOOKUP_QUERY:

For Each GR In Me.LOOKUP_QUERY

And only LOOUP_QUERY items that appear in screen are retrieved from loop.

Sample:

MAIN_QUERY = ORDER LINE (QTY, Product, Order, ...)
LOOKUP_QUERY = PRODUCT (NAME, ...)

For Each P In Me.PRODUCT

Only retrieve product that appear in selected order, I need ALL products.

Notice that I can disable page, but if order does not contains at least a line for each product some products will be missed on export.

Was it helpful?

Solution

Answerd by Matt Evans at Visual Studio LightSwitch - General Questions forum:

you want to look at the entityset, which corresponds to the data on the server. The VisualCollection is the backing store for the grids on the screen.

if you have an entity named "Customer", in your intrinsic database (ApplicationData) this would show customers:

for each c in Me.Dataworkspace.ApplicationData.Customers

    rowCount += 1
    excel.Cells(rowcount, 1).Value = c.Name

OTHER TIPS

If you use a query that has parameters to populate your grid, and you've enabled paging, then the grid will only have one "page" of data in it at any time. This is why you're not getting ALL of the records.

  1. The easiest way is to disable paging on the collection, but of course if there's a large number of records it may not be a good idea to do so. Then again you want to export all of the records anyway, so they'll all be pulled down to the client, whether you page them or not.

  2. Another way is to programatically sequence the pages in your code (but you may as well do method one).

  3. Or you can use the same query that populates the grid, but in code instead of using the grid's collection (& again you're still pulling all the records down to the client).

Yann, I know this has been thread has been a while but I just want to clarify one thing. If I need to iterate through every record in this grid query (which has pagination), I can do either of these:

  1. Create another screen query (with same filters but NO pagination) and use it for the iteration
  2. Go to the EntitySet (i.e. DataWorkspace.ApplicationData.TableName), apply the appropriate filters and iterate from there

Thanks for all your contribution to LS.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top