Let's say I have a list of 100 items with an Id ranging from 1 to 100. I put them in a QCV, ordered by Id, with a page-size of 20 resulting in 5 pages. I bind that QCV to a GridView and a DataPager. Page 1 is displayed which means Ids 1 to 20 are visible in the Grid.
Question: what is the most efficient way to find and display the item with a specific Id without loading all pages and skipping through all items in a page? Please give an example!
5 Answers, 1 is accepted
For achieving this, you can try using the GetItemAt() method that QueryableCollectionView exposes. Since it expects the index of a given object to be passed to it, you should be able to use an approach similar to the one below.
An important note is, that you need to ensure that you are searching for an item present within the current page.
Hope it helps.
this sentence is exactly the problem: "An important note is, that you need to ensure that you are searching for an item present within the current page." As for your example, imagine "Liverpool" is on page 10, but current page of QueryableCollectionView is 2. How do I know which page to load? I need an algorithm to locate the page where my item is located! Since there is a MoveToPage method this should be easy as long as I know the index of the page.
Thanks for the update.
Since you confirmed using a List collection for you items, you can use its IndexOf() method. Having the index of a given item and the value set to the PageSize property of the QueryableCollectionView, you should be able to implement the required calculation and load the needed Page.
Would such approach fit your requirements?
Well, "List" is normally coming from a database and represents a list of entities in a specific DbSet (when using EntityFramework 6.1). Then the QCV gets filtered, sorted etc. by the user, especially when bound to a Grid. So the original list or DbSet soon has nothing to do with the entries in the QCV.
What I do at the moment is search in the current QCV, if not found move to first page and search again. If not found move to next page until found. Unfortunately QCV does not tell me "you are at the last page". I can "MoveToNextPage()" and it still returns "true", PageIndex remains the same. Shouldn't it return a "false" if at the last page and I try to move to the next page? Is there a way to detemine if I am at the last page?
Indeed, the QCV will contain only the items that have passed the filtering criteria and are on the current page. If you want to find a particular item, you will have to perform the filtering on the source collection (or its copy), find the item and calculate the page it is on based on tha page size. All those operations should be performed on the original collection since RadGridView know only about th items on the page that's currently displayed.