Using LINQ to locate Item in GridView

4 posts, 1 answers
  1. Raymond
    Raymond avatar
    65 posts
    Member since:
    Oct 2013

    Posted 28 Apr 2014 Link to this post

    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...
  2. Answer
    Boris
    Admin
    Boris avatar
    276 posts

    Posted 30 Apr 2014 Link to this post

    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.
     
  3. Raymond
    Raymond avatar
    65 posts
    Member since:
    Oct 2013

    Posted 04 May 2014 in reply to Boris Link to this post

    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...
  4. Raymond
    Raymond avatar
    65 posts
    Member since:
    Oct 2013

    Posted 14 May 2014 in reply to Boris Link to this post

    Ho Boris,

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

    Thanks Ray
Back to Top