The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

How to get the Index of an Entity within a Collection?

Last post 08-18-2007, 1:04 PM by netjet. 4 replies.
Sort Posts: Previous Next
  •  08-17-2007, 3:00 PM 4600

    How to get the Index of an Entity within a Collection?

    - ES Version: 2007 - Trial version
    -DB: MSSQL 2005 Express 

    Here is what I want to accomplish:

    • A page will display 3 DetailsView Controls bound to the same ESDataSource. These DetailsView Controls get their data from the same table. They are separated to facilitate formatting and printing. – Done and working fine.
    •  Custom Paging – The 3 Controls must show the same record. – Done and working fine.

    - Next” and “Back” buttons allow navigation. Gridview1.DataItemIndex is used to accomplish that.

     

    Code:
    // Code for the Next button:
    
     DetailsView1.PageIndex = DetailsView1.DataItemIndex + 1;
    •  Each Individual DetailsView Control should have editing capability. – Done and working fine.
    - Accomplished through EsDataSource.
    •  User should be able to type a record ID in a text box, click “find” and DetailView Controls should display the selected data. Here is where I need help…Huh?

     - I believe this is not possible through DetailsView: The properties SelectedValue, DataItem, DataKey and Page are read only.

    • Here is my question: Can I accomplish this using the ES Collection? I have tried using collection.Filter and collection.Query methods. They will bring up the correct record, but they will mess with my customized paging.
    Code:
    1    // Code in ES_Select
    2    
    3    CustCustomersCollection CustColl = new CustCustomersCollection();
    4    
    5    // CustomerID value comes from Textbox and is added to EsDataSource state through "Find Button"
    6    
    7             if (EsDataSource1.State.ContainsKey("CustomerID"))
    8             {
    9                 
    10                CustColl.Filter = "CustomerID=" +(int)EsDataSource1.State["CustomerID"];          
    11    
    12   // Tried Query.Where, search works fine, but messes with custom paging
    13   
    14   //            CustColl.Query.Where(CustColl.Query.CustomerID == (int)EsDataSource1.State["CustomerID"]);
    15   
    16                CustColl.Query.Load();
    17                e.Collection = CustColl;
    18                EsDataSource1.State.Clear();
    19   
    20            }
    21   
    22            else
    23            {
    24                CustColl.Query.Load();
    25                e.Collection = CustColl;
    26   
    27            }
    

    I’m thinking that if I can get the Index of an entity within a collection I could pass that index to the DetailsView1.DataItemIndex property and solve my problem. Is it possible to pass a parameter to the Collection (Say, record ID=10 and get the Index of that record within the collection?)

     I’m open to suggestions if you think there is a better way to accomplish this. Thanks and your help is appreciated.


    It is all about the Code...
  •  08-17-2007, 4:39 PM 4601 in reply to 4600

    Re: How to get the Index of an Entity within a Collection?

    The Collections have a FindByPrimaryKey() method, perhaps that's your ticket?

    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  08-18-2007, 9:56 AM 4613 in reply to 4601

    Re: How to get the Index of an Entity within a Collection?

    Mike, thanks for your help.

    I spent the morning trying to get this to work using FindByPrimaryKey() but not matter what I did, the entity value was always null...

    Here is an example:

     

    Code:
    1    Customers entity = Collection.FindByPrimaryKey(21);
    

    PK 21 does exist in my database... What am I doing wrong?

     


    It is all about the Code...
  •  08-18-2007, 10:10 AM 4614 in reply to 4613

    Re: How to get the Index of an Entity within a Collection?

    It's Find not Load, that means it had to already exist in the collection. I thought you were trying to find a particular entity in the collection?

    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  08-18-2007, 1:04 PM 4617 in reply to 4614

    Re: How to get the Index of an Entity within a Collection?

    Mike, 

    No sure I understand your post. I did use Find and yes, I am trying to find a particular entity in the collection.

    I tried to use FindByPrimaryKey(), but was not able to pass that value to the DetailsView control.

    I used it as described in this post: http://community.entityspaces.net/forums/thread/1601.aspx , then tried to pass the value of the entity to the query.load() method and bind it to the gridview. When debugging the application, VS2005 showed that null was passed to the entity from the FindByPrimaryKey() method.

     

    Thanks. 


    It is all about the Code...
View as RSS news feed in XML