Getting Index of a specific row??

4 posts, 0 answers
  1. Gabriel
    Gabriel avatar
    4 posts
    Member since:
    Aug 2010

    Posted 04 Dec 2010 Link to this post

    I've got  a gridview and a persistent class which is binded to the gridview via ObjectView. I want to search for a specific row in a specific condition then focus to that row. For doing that I used the following code to find the specific row, now I want to find the index of the result in the grid so that I can focus on the specific row in the grid:
    IObjectScope scope = ObjectScopeProvider1.ObjectScope();
    var result = from c in scope.Extent<MyPersistentClass>()
                 where c.Id == 1
                 select c;

    I don't know how to use IndexOf method of ObjectView, every time I use this function I get -1 as a result.
    What should I do??
  2. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 06 Dec 2010 Link to this post

    Hello Gabriel,

    The reason for this behavior is that the IObjectScope instance that is used for retrieving the persistent object is not the same as the IObjectScope instance used to bind the GridView.
    You could use the following approach to work with the correct IObjectScope object:

    IObjectScope scope = this.ObjectProvider1.Context as IObjectScope;
    if (scope != null)
    {
        MyPersistentClass result  = scope.Extent<MyPersistentClass>().FirstOrDefault(x => x.ID == 1);
        if (result  != null)
        {
            int index = this.ObjectView1.IndexOf(result);
            ...
        }
    }

    Hope that helps.

    Regards,
    Damyan Bogoev
    the Telerik team
    Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
  3. DevCraft banner
  4. Gabriel
    Gabriel avatar
    4 posts
    Member since:
    Aug 2010

    Posted 06 Dec 2010 Link to this post

    Thanks, that worked.
    But what if I have multiple results instead of just one result, and I want to navigate trough the results. That's why I used this query:
    var result = from c in scope.Extent<MyPersistentClass>()
                 where c.Id == 1
                 select c;
    // or maybe this:
    var result = from c in scope.Extent<MyPersistentClass>()
                 where c.Name == "some text"
                 select c;
  5. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 08 Dec 2010 Link to this post

    Hello Gabriel,

    You could store the result from the query within a collection:

    List<MyPersistentClass> result = from c in scope.Extent<MyPersistentClass>()
                                     where c.FilterField = filterValue
                                     select c;

    Later you will be able to iterate over this list and retrieve the index of the current object from the list using the
    approach proposed in the previous post.
    Hope that helps.

    Greetings,
    Damyan Bogoev
    the Telerik team
    Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
Back to Top