This is a migrated thread and some comments may be shown as answers.

Locate Item in paged QueryableCollectionView

5 Answers 61 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Heiko
Top achievements
Rank 1
Veteran
Heiko asked on 08 Feb 2016, 03:38 PM

Hi!

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!

Regards
Heiko

5 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 11 Feb 2016, 03:19 PM
Hello Heiko,

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.
var qcv = (this.clubsGrid.ItemsSource as QueryableCollectionView);
 
var entity = (qcv.SourceCollection as IEnumerable<Club>)
    .Where(c => c.Name == "Liverpool").FirstOrDefault();
 
int index = qcv.IndexOf(entity);
 
if (index != -1)
{
    var item = qcv.GetItemAt(index);
}
‚Äč
An important note is, that you need to ensure that you are searching for an item present within the current page.

Hope it helps.

Regards,
Stefan X1
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Heiko
Top achievements
Rank 1
Veteran
answered on 15 Feb 2016, 06:10 PM

Hello Stefan,

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.

Regards
Heiko

0
Stefan
Telerik team
answered on 16 Feb 2016, 03:53 PM
Hi Heiko,

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?

Best Regards,
Stefan X1
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Heiko
Top achievements
Rank 1
Veteran
answered on 02 Mar 2016, 09:25 AM

Hi Stefan!

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?

Regards
Heiko

0
Maya
Telerik team
answered on 04 Mar 2016, 12:51 PM
Hello Heiko,

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.

Regards,
Maya
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Heiko
Top achievements
Rank 1
Veteran
Answers by
Stefan
Telerik team
Heiko
Top achievements
Rank 1
Veteran
Maya
Telerik team
Share this question
or