I have a RIA services application I'm developing and am having problems with server side paging and the display of paged items in the DataGrid. I'm using a custom DomainService where the PageIndex & PageSize is passed via the Query along with several filter parameters.
The problem is my first page of results is displayed properly, but when I page to the next set of results, my items are tacked onto the end of the list instead of just showing the new results returned from the server. Ie, my DataGrid is showing all of the cumulative objects returned from the server instead of just displaying the latest page of results returned.
First, I'm using a DomainCollectionView and DomainCollectionViewLoader:
_context = new StudyQueryContext();
_entityList = new EntityList(_context.PortalStudies);
_loader = new DomainCollectionViewLoader(Load, LoadComplete);
PortalStudies = new DomainCollectionView(_loader, _context.PortalStudies);
Then I have the fairly standard Load() and LoadComplete() methods:
public LoadOperation Load()
{
if (IsLoading)
{
return null;
}
IsLoading = true;
EntityQuery q = _context.GetPortalStudyQuery(PatientsName, PatientId, AccessionNumber, PortalStudies.PageIndex, PortalStudies.PageSize);
return _context.Load(q);
}
private void LoadComplete(LoadOperation op)
{
if (op.HasError)
{
System.Windows.MessageBox.Show(op.Error.ToString(), "Search Error", System.Windows.MessageBoxButton.OK);
op.MarkErrorAsHandled();
}
else if (!op.IsCanceled)
{
_entityList.Source = op.Entities;
_context.PortalCountAll(PatientsName, PatientId, AccessionNumber, CountAllComplete, null);
}
IsLoading = false;
}
Note that I assign TotalItemCount when the PortalCountAll method returns.
Just for completeness sake, the signature of my DomainService Query method is the following:
[Query]
public IEnumerable<PortalStudy> GetPortalStudy(string patientsName, string patientId, string accessionNumber, int startIndex, int pageSize)
{ }
I'd assume the issue is with how I'm setting up _entityList and with the assignment of the results in the LoadComplete() method:
_entityList.Source = op.Entities;
It seems that in all the LINQ based examples, this assignment seems to allow for only the current results to be displayed in the DataGrid. For some reason this doesn't seem to be the case when using a custom query method.
I'm wondering what is the preferred way to clear the DataGrid when I change the page? I know I can just call _context.PortalStudies.Clear() to clear the results before I query, but this results in a flash on the page with the items cleared and not populated again until the query returns from the server. What is the proper way to display just the current page of results when using a custom DomainService?
You can (and should) pass the PageIndex and PageSize when working with a custom ORM that does not support LINQ. It will allow you to page against the ORM, but will also slightly complicate paging. You'll either need to override DomainService.Count or add a totalCount out parameter.
In your case, however, it looks like the bug is that you've passed the EntitySet (_context.PortalStudies) to the DCV constructor instead of the EntityList (_entityList).