ComponentOne Data Source for Entity Framework
Design-Time Features / Large Datasets: Paging
In This Topic
    Large Datasets: Paging
    In This Topic

    To show large amounts of data without bringing it all to the client at once, applications have traditionally used paging. Paging is not an ideal solution; it complicates the interface and makes it less convenient for the user, but it is preferred in some applications. For these cases, C1DataSource supports paging as expected, but it does so without the traditional limitations on data modification. The user can make changes in multiple pages in one session without being forced to send the changes from one page to the database before moving to the next page. That’s a substantial enhancement compared to other paging implementations, such as the one in DomainDataSource in Microsoft RIA Services.

    Note: DataSource for Entity Framework does offer a solution to the drawbacks of paging; we'll cover Virtual Mode later in this documentation.

    To implement paging, follow these steps:

    1. Using the project we created to demonstrate Master-Detail Binding, add a new form with a C1DataSource component using the same ContextType as before. Note that you can make this the startup form to save time when you run the project.
    2. Create a ViewSource in the ViewSourceCollection editor, entering Orders as the EntitySetName.
    3. To enable paging, set the PageSize property to 10 for now, but you can choose any reasonable value for this property. It’s simply determining the number of data rows that will be displayed on a page.
    4. Add a DataGrid to the form and set its DataSource property to the C1DataSource and its DataMember property to Orders.
    5. To facilitate easy movement between pages, add two buttons, btnPrevPage and btnNextPage, and a label, labelPage, as shown in the following image.

    6. Add the following code containing a RefreshPageInfo handler for the PropertyChanged event used to show the current page number and page count, and handlers for button Click events used to move to the next and previous pages:

      To write code in Visual Basic

      Visual Basic
      Copy Code

      Imports C1.Data.DataSource

       Public Class Paging
           Private _view As ClientCollectionView

           Public Sub New()

               ' This call is required by the designer.
               InitializeComponent()

               ' Add any initialization after the InitializeComponent() call.
               _view = C1DataSource1("Orders")

               RefreshPageInfo()

               AddHandler _view.PropertyChanged, AddressOf RefreshPageInfo

           End Sub

           Private Sub RefreshPageInfo()
               labelPage.Text = String.Format("Page: {0} / {1}", _view.PageIndex + 1, _view.PageCount)
           End Sub

           Private Sub btnPrevPage_Click(sender As System.Object, e As System.EventArgs) Handles btnPrevPage.Click
               _view.MoveToPreviousPage()
           End Sub

           Private Sub btnNextPage_Click(sender As System.Object, e As System.EventArgs) Handles btnNextPage.Click
               _view.MoveToNextPage()
           End Sub
       End Class

      To write code in C#

      C#
      Copy Code

      namespace TutorialsWinForms
       {
           public partial class Paging : Form
           {
               ClientCollectionView _view;

               public Paging()
               {
                   InitializeComponent();

                   _view = c1DataSource1["Orders"];

                   RefreshPageInfo();
                   _view.PropertyChanged += delegate { RefreshPageInfo(); };
               }

               private void RefreshPageInfo()
               {
                   labelPage.Text = string.Format("Page: {0} / {1}", _view.PageIndex + 1, _view.PageCount);
               }

               private void btnPrevPage_Click(object sender, EventArgs e)
               {
                   _view.MoveToPreviousPage();
               }

               private void btnNextPage_Click(object sender, EventArgs e)
               {
                   _view.MoveToNextPage();
               }
           }
       }

    7. Save, build and run the application. Page through the Orders. While you are moving between pages, try changing some data in the grid. Try changing data in one page, and then move to another page and try changing data there. Notice that C1DataSource allows you to do this without forcing you to save data to the database before leaving the page. This is an important enhancement compared with other paging implementations, including the one supported by DomainDataSource in Microsoft RIA Services (which is for Silverlight only, whereas C1DataSource supports this, as other features, for all three platforms: WinForms, WPF, Silverlight).

    Try also to delete some orders. This is also allowed without restrictions, and moreover, the current page will automatically complete itself to keep the number of rows in the page unchanged. You can also add new rows. They are added to the end of the dataset.

    And if you add a Save Changes button, using the following code as we did previously, then you will be able to save changes made in multiple pages by pressing that button when you are done.

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Private Sub btnSaveChanges_Click(sender As System.Object, e As System.EventArgs)
    C1DataSource1.ClientCache.SaveChanges()
    End Sub

    To write code in C#

    C#
    Copy Code
    private voidbtnSaveChanges_Click(object sender, EventArgs e)
     {
        c1DataSource1.ClientCache.SaveChanges();
     }

    All this functionality is what you would expect from a paging implementation that supports unrestricted data modification. Unfortunately, it is not easy to implement. For example, think of all possible cases where changing data on one page interferes with what should be shown on other pages, and so on. That is why paging usually imposes severe restrictions on data modifications, if they are at all allowed. For example, MS DomainDataSource requires you to save all changes before changing pages. Fortunately, C1DataSource supports paging without restricting data modifications in any way.

    As with many other features, unlimited modifiable paging is made possible by the client-side cache, see The Power of Client Data Cache. That also means that paging implementation in C1DataSource is optimized both for performance and for memory consumption. The cache provides that optimization. It keeps recently visited pages in memory, so re-visiting them is usually instantaneous. And it manages memory resources, releasing old pages when necessary, to prevent memory leaks.