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

Using LINQ to locate Item in GridView

3 Answers 200 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Raymond
Top achievements
Rank 1
Raymond asked on 28 Apr 2014, 09:32 PM
I am using the following to locate an Item in the grid and select it;
For Each item As AreaView In gvArea.Items
    If (item.AreaId = iAreaId) Then
        gvArea.SelectedItem = item
        gvArea.ScrollIntoView(item)
        Exit For
    End If
Next

However I would prefer to use something like;

Dim item As AreaView = gvArea.Items.Where(Function(f) f.AreaId = iAreaId)
gvArea.SelectedItem = item
gvArea.ScrollIntoView(item)

I have tried the above using "gvArea.Items", "gvArea.ItemsSource" and "gvArea.DataContext" with no luck.

Thanks in advance...

3 Answers, 1 is accepted

Sort by
0
Accepted
Boris
Telerik team
answered on 30 Apr 2014, 02:28 PM
Hello Raymond,

You can use a lambda expression to traverse the ItemsSource collection of the GridView. Please keep in mind that you will need to cast the collection to the type of the property which provides it with the data.

Area item = (this.xAreasGridView.ItemsSource as ObservableCollection<Area>).FirstOrDefault(f => f.Id == iAreaId);
 
            if (item != null)
            {
                this.xAreasGridView.SelectedItem = item;
                this.xAreasGridView.ScrollIntoView(item);
            }

In this case I have a ObservableCollcetion<Area> Areas property in the ViewModel that is bound to the ItemsSource property of the GridView.

I hope this helps.

Regards,
Boris Penev
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Raymond
Top achievements
Rank 1
answered on 04 May 2014, 11:27 PM
Hi Boris,

Thanks for getting back to me on this.

I realised however that since my GridView is bound to an ObservableCollection I can use;

Dim item As AreaView = colAreas.Where(Function(f) f.AreaId = iAreaId)
gvArea.SelectedItem = item
gvArea.ScrollIntoView(item)

Your solution of course would also work.

Thanks for your help...
0
Raymond
Top achievements
Rank 1
answered on 15 May 2014, 03:50 AM
Ho Boris,

Just found an instance in my code where your solution is applicable - works a treat :)

Thanks Ray
Tags
GridView
Asked by
Raymond
Top achievements
Rank 1
Answers by
Boris
Telerik team
Raymond
Top achievements
Rank 1
Share this question
or