Monday, July 16, 2012

LightSwitch: How to get Total Number of Records in all pages in a Multi-Page DataGrid

Level: Intermediate

Knowledge Required:
  • LightSwitch
  • Search Screen
  • DataGrid
  • Creating a new property and placing it on Screen

This post explains how to get the total number of records in all the pages of Multi-Page DataGrid.

Recently I started to develop a software on Microsoft LightSwitch, I found it interesting and a very effective tool to deliver a good looking business application in no time.

I learned lots of stuff and was thinking to write about it. But unfortunately due to time constraint I couldn't.

Anyway while developing the application I was encountered a requirement to display the number of records at the bottom of DataGrid. While Googling this thing, I couldn't find a proper solution, so I decided to put it here for others.

Basically my solution does NOT display the accurate number of records. What I did is just to calculate the number of records by looking at Page Count, Page Size and Current Page.

So these are the steps which I did,

  • On my Search Screen, I created a new String Property, let say TotalRecords
  • Placed this property as Label Control after the DataGrid control
  • Write code in Query Loaded method

To write a code in the Query Loaded method, you first need to select your query in Screen Designer, then select the loaded method as shown in the following image,

And then put the following code,

    partial void SearchApplication_Loaded(bool succeeded)
        this.TotalRecords = this.GetPageCountString(
                                this.SearchApplication.Details.PageNumber, this.SearchApplication.Details.PageCount,
                                this.SearchApplication.Details.PageSize, this.SearchApplication.Count);

    private string GetPageCountString(int iCurrentPage, int iTotalPages, int iPageSize, int iCurrentPageRows)
        if (iTotalPages > 1)
            int iBeforeLastPage;
            iBeforeLastPage = (iTotalPages - 1) * iPageSize;

            if (iCurrentPage < iTotalPages)
                return iBeforeLastPage.ToString() + "+";
                return (iBeforeLastPage + iCurrentPageRows).ToString();
            return iCurrentPageRows.ToString();
Note that I've used the Query's Details Property to get the Current Page, Total Page Count and Page Size, this is simple calculation. One more thing, in my application, my search query name was SearchApplication, you may have a different Query name, so the loaded method most probably differ on your side.

1 comment:


For take all rows. This work

If (Me.EquipmentAllQuery.Details.PageCount > 1) Then
Me.EquipmentAllQuery.Details.PageNumber = Me.EquipmentAllQuery.Details.PageCount
Dim LastPageRows As Integer = Me.EquipmentAllQuery.Count()
Me.EquipmentAllQuery.Details.PageNumber = 1
Quantidade1 = (Me.EquipmentAllQuery.Details.PageCount - 1) * Me.EquipmentAllQuery.Details.PageSize + LastPageRows
Quantidade1 = Me.EquipmentAllQuery.Count()
End If