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

get/set first item in queryablecollectionview

7 Answers 207 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Scott
Top achievements
Rank 1
Scott asked on 02 May 2011, 07:48 PM
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

7 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 03 May 2011, 06:00 AM
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
0
Scott
Top achievements
Rank 1
answered on 03 May 2011, 04:53 PM
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.
0
Vlad
Telerik team
answered on 04 May 2011, 06:58 AM
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
0
Scott
Top achievements
Rank 1
answered on 04 May 2011, 03:42 PM
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
0
Rossen Hristov
Telerik team
answered on 04 May 2011, 04:27 PM
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
0
Scott
Top achievements
Rank 1
answered on 04 May 2011, 04:33 PM
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.
0
Rossen Hristov
Telerik team
answered on 06 May 2011, 08:18 AM
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
Tags
General Discussions
Asked by
Scott
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Scott
Top achievements
Rank 1
Vlad
Telerik team
Share this question
or