DataSource for Entity Framework in WPF > Large DataSets: Paging |
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: EF DataSource does offer a solution to the drawbacks of paging; we'll cover virtual mode|document=WordDocuments\C1DataStudio-WPF.docx;topic=Large Datasets\: Virtual Mode later in this documentation. |
To implement paging, follow these steps:
Using the project we created to demonstrate Master-Detail Binding|document=WordDocuments\C1DataStudio-WPF.docx;topic=Master-Detail Binding, add a new form with a C1DataSource component using the same ObjectContextType as before. Note that you can make this the startup form to save time when you run the project.
ItemsSource="{Binding ElementName=c1DataSource1, Path=Orders}"
<StackPanel Orientation="Horizontal" Grid.Row="1" Margin="2">
<Button Padding="10,0,10,0" Margin="1" Content="<" Click="MoveToPrevPage"/>
<TextBlock VerticalAlignment="Center" Text="Page: "/>
<TextBlock VerticalAlignment="Center" x:Name="pageInfo"/>
<Button Padding="10,0,10,0" Margin="1" Content=">" Click="MoveToNextPage"/>
</StackPanel>
Visual Basic Copy Code Imports C1.Data.DataSource Public Class Paging Private _view As ClientCollectionView Public Sub New() InitializeComponent() _view = C1DataSource1("Orders") RefreshPageInfo() AddHandler _view.PropertyChanged, AddressOf RefreshPageInfo End Sub Private Sub RefreshPageInfo() pageInfo.Text = String.Format("{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
C# Copy Code using C1.Data.DataSource; namespace TutorialsWPF { public partial class Paging : Window { ClientCollectionView _view; public Paging() { InitializeComponent(); _view = c1DataSource1["Orders"]; RefreshPageInfo(); _view.PropertyChanged += delegate { RefreshPageInfo(); }; } private void RefreshPageInfo() { labelPage.Text = string.Format("{0} / {1}", _view.PageIndex + 1, _view.PageCount); } private void MoveToPrevPage(object sender, RoutedEventArgs e) { _view.MoveToPreviousPage(); } private void MoveToNextPage(object sender, RoutedEventArgs e) { _view.MoveToNextPage(); } } }
Visual Basic Copy Code Private Sub btnSaveChanges_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) c1DataSource1.ClientCache.SaveChanges() End Sub
C# Copy Code private void btnSaveChanges_Click(object sender, RoutedEventArgs 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, Microsoft DomainDataSource requires you to save all changes before changing pages. Fortunately, EF DataSource 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 EF DataSource 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.