get/set first item in queryablecollectionview

8 posts, 0 answers
  1. Scott
    Scott avatar
    13 posts
    Member since:
    Apr 2011

    Posted 02 May 2011 Link to this post

    When I sort a RadGridView bound to a QueryableCollectionView, I want to select the first item in the grid within the MVVM pattern. In code behind, this is obviously pretty easy.

    Currently I have an EventToCommand that fires on the Sorted event of the RGV. I then call a function called GridSorted().

    As I see it, there are two options...

    1) Set my SelectedEmployee property (which the RGV SelectedItem is bound to)
    2) "Select" the first item in the collection, as its sorted in the grid.

    Any ideas on how to do either?

    SS
  2. Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 03 May 2011 Link to this post

    Hello Scott,

    I think I could not understand what the problem is.

    What is stopping you from setting the SelectedItem property of RadGridView? Could you please explain?

    Here is the article concerning selection. I believe that it can help. Please, take a look at it in case you have not.

    Best wishes,
    Ross
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  3. Scott
    Scott avatar
    13 posts
    Member since:
    Apr 2011

    Posted 03 May 2011 Link to this post

    I am trying to avoid code behind because I'm using MVVM. I know how to do it in code behind no sweat. I simply want to know if there's a way to select the first item of my queryablecollectionview.... and not just the first item of the sourcecollection, but the first item displayed after a filter/sort or when using pagination.
  4. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 04 May 2011 Link to this post

    Hi,

     When the grid is bound to QueryableCollectionView every collection setting will be transfered to the grid automatically - you can safely do everything you want in your data layer and the grid will respect this immediately. 

    Best wishes,
    Vlad
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  5. Scott
    Scott avatar
    13 posts
    Member since:
    Apr 2011

    Posted 04 May 2011 Link to this post

    Vlad -

    Sure, I understand the binding concepts quite well. What I don't understand is how to select not just the first item in a QueryableCollectionView, but the first item within the context of my sorting/filtering/paging. If I select the first item in the sourceCollection, it selects the first item in the initial query. What I want would be the first item in my newly sorted/filtered/paged list.... so whenever a user fires off any of these functions, the new dataset auto-selects the first item in this new set.

    Does that make sense?

    Thanks for your help,

    SS
  6. Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 04 May 2011 Link to this post

    Hello Scott,

    The first item in the QueryableCollectionView is in fact the first item within the context of your sorting/filtering/paging.

    That is why it is called a View. It is a view over a source collection that applies your sorting/filtering/paging. 

    So imagine that your source collection is "1, 2, 3" and you have a filter that says "give me only the even numbers". The first item in the QueryableCollectionView will be 2. So this is the item that you are looking for.

    I hope this makes sense.

    Kind regards,
    Ross
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  7. Scott
    Scott avatar
    13 posts
    Member since:
    Apr 2011

    Posted 04 May 2011 Link to this post

    Hey Ross - It makes total sense, which has never been the issue, but I don't know the code to use to actually select the 1st item programmatically.

    When the user fires off a filter, obviously my view changes, displaying the first 10 items in my filtered collection (RadDataPager.PageSize = 10). I'm using a dataform to show details of the items in my RadGridView, and I'd like to auto-select the 1st item in this collection. In Codebehind, this would be simple:

    private void GV1_Sorted(object sender, Telerik.Windows.Controls.GridViewSortedEventArgs e)
            {
                if (GV1.Items.Count > 0)
                {
                    GV1.SelectedItem = this.GV1.Items[0];
                }
            }

    That's obviously one way to do it. I'd like to avoid code behind, so I'm hoping to do this in my ViewModel. I'm successfully capturing the 'Sorted','Filtered' and 'SelectedCellsChanged' events. I have a property in my VM named 'SelectedEmployee' that's bound to my gridview & dataform. When somebody Sorts/Filters, I'd like to set my SelectedEmployee to the 1st item in my newly filtered/sorted list. How do I do this? It would be something like SelectedEmployee = (Model.Employee)EmployeeQCV.Items[0]. Obviously that line doesn't work, but I don't know what to put in place of the (Model.Employee)EmployeeQCV.Items[0].

    Thanks for your help.
  8. Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 06 May 2011 Link to this post

    Hi Scott,

    Only lists have indexers, i.e. this.myList[0];

    To learn how to select the first element of an IEnumerable, please search for the following phrase in Google:

    "how do I select the first element of an IEnumerable"

    The first search result that appears explains everything. The other results are also very helpful.

    Personally, I would use the First() LINQ method, since I find it most convenient. To see all other Enumerable methods, please take a look at this article.

    To learn what is the difference between an array, a list, and an IEnumerable, please take a look at this article.

    I hope this helps.

    Kind regards,
    Ross
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
Back to Top