HomeSilverlight Silverlight 4.0: Paging Through Data using...
Silverlight 4.0: Paging Through Data using DomainDataSource
This article gives you a great introduction to how to page through the objects available in the “DomainDataSource” control using Silverlight (with WCF RIA Services).
To make this article simple, I managed to create a simple Silverlight 4.0 application which consumes a WCF RIA Service created using WCF RIA Service Library. If you are not familiar with developing applications using the WCF RIA Service Library and Silverlight 4.0, check out a beginner's article.
The entire source code for this article is available in the form of a free downloadable zip file. The solution was developed using Microsoft Visual Studio 2010 Ultimate Edition with Microsoft Silverlight 4.0 on Windows 7 Ultimate edition. I didn't really test it in any other environment. I request that you post in the discussion area if you have any problems with execution.
Paging data using DataPager and DomainDataSource: design
In my previous articles, I explained the importance of DomainDataSource and how we can query, sort, group, filter and navigate data using the same in a Silverlight 4.0 application. In this section, we will focus on paging data using DataPager together with DomainDataSource.
The above creates a grid layout with two rows. I would like to reserve the first row for the pager (I will explain this in the next paragraph) and the second for data grid. Proceeding further, we have the following:
In previous sections, we have seen how to paginate data using DataPager together with DomainDataSource. In this section, we are going to achieve the same, but without using DataPager.
The above design is similar to what we have already done in previous sections, except that we replaced DataPager with a stack panel which contains a group of buttons placed next to each other using horizontal orientation.
The design would look similar to the following:
The next section contains the code-behind for the above design.
Paging data through DomainDataSource and without using DataPager: code-behind
This section contains the code-behind for the markup provided in the previous section. The following is the code:
Partial Public Class DdsPagingCodeBehind Inherits UserControl
Public Sub New() InitializeComponent() End Sub
Private Sub EmpDomDataSource_LoadedData(ByVal sender As Object, ByVal e As System.Windows.Controls.LoadedDataEventArgs) Handles EmpDomDataSource.LoadedData If e.HasError Then System.Windows.MessageBox.Show(e.Error.ToString, "Load Error", System.Windows.MessageBoxButton.OK) e.MarkErrorAsHandled() End If End Sub
Private Sub btnFirstPage_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnFirstPage.Click Me.EmpDomDataSource.DataView.MoveToFirstPage() End Sub
Private Sub btnPreviousPage_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnPreviousPage.Click Me.EmpDomDataSource.DataView.MoveToPreviousPage() End Sub
Private Sub btnNextPage_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnNextPage.Click Me.EmpDomDataSource.DataView.MoveToNextPage() End Sub
Private Sub btnLastPage_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnLastPage.Click Me.EmpDomDataSource.DataView.MoveToLastPage() End Sub
End Class
Once the form is executed, we should see an output similar to the following:
The next section gives an explanation for the above code.
Paging data through DomainDataSource and without using DataPager: code-behind explanation
In my previous article, I described the "CurrentItem" and "DataView" properties available as part of the DomainDataSource object.
Pagination using DomainDataSource can be easily achieved using the following methods (available with "DataView" property of DomainDataSource):
MoveToFirstPage
MoveToNextPage
MoveToPreviousPage
MoveToLastPage
MoveToPage
The first four methods from the above list would give us respective entity objects from the current position. "MoveToPage" is a special case, where we can directly land on a specific page by specifying the page number.
Let us test the "MoveToPage" method. Modify the stack panel present in the previous markup in such a way that it includes two more controls, as shown below:
Private Sub btnGoTo_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnGoTo.Click If Me.txtGotoPos.Text > Me.EmpDomDataSource.DataView.PageCount - 1 OrElse Me.txtGotoPos.Text < 0 Then MessageBox.Show("Invalid Page number") Else Me.EmpDomDataSource.DataView.MoveToPage(Me.txtGotoPos.Text) End If End Sub
Finally, the output would look similar to the following:
Also, make sure that the page index starts with zero, and my code works accordingly. You may have to modify the logic if it has to start with one.
My upcoming articles will focus more on using the "DomainDataSource" element. I hope you enjoyed the article and any suggestions, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.com/