Populating a DropDown with an entity collection

Posts   
 
    
niti
User
Posts: 7
Joined: 28-Jun-2005
# Posted on: 28-Jun-2005 20:55:34   

Hi!

I am trying to bind an entity collection to a drop down list in C#, like this (entities generated with LLBLGen Pro, of course):

Object all = null; // declare collection of type user: all = new UserCollection(); // popluate it: it works, i can see it has 182 items; (all as UserCollection).GetMulti(null); // assign the collection to the drop down data source: this.ddlUsers.DataSource = all; // assigning the text field: this.ddlUsers.DataTextField = (all as UserCollection).Items["Name"].ToString(); // assigning the key: this.ddlUsers.DataValueField = (all as UserCollection).Items["UserId"].ToString(); // binding the collection to the control: this.ddlUsers.DataBind();

I get an error on Items, and i can see that i cannot access the field.

Any suggetions?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 28-Jun-2005 21:21:44   

niti wrote:

Hi!

I am trying to bind an entity collection to a drop down list in C#, like this (entities generated with LLBLGen Pro, of course):

Object all = null; // declare collection of type user: all = new UserCollection(); // popluate it: it works, i can see it has 182 items; (all as UserCollection).GetMulti(null); // assign the collection to the drop down data source: this.ddlUsers.DataSource = all; // assigning the text field: this.ddlUsers.DataTextField = (all as UserCollection).Items["Name"].ToString(); // assigning the key: this.ddlUsers.DataValueField = (all as UserCollection).Items["UserId"].ToString(); // binding the collection to the control: this.ddlUsers.DataBind();

I get an error on Items, and i can see that i cannot access the field.

Any suggetions?

Shouldn't the code be:



UserCollection all = new UserCollection();
// popluate it: it works, i can see it has 182 items;
all.GetMulti(null);
// assign the collection to the drop down data source:
this.ddlUsers.DataSource = all;
// assigning the text field:
this.ddlUsers.DataTextField  = "Name";
// assigning the key:
this.ddlUsers.DataValueField = "UserId";
// binding the collection to the control:
this.ddlUsers.DataBind();

(I removed the 'as' usage, as that's not really necessary, neither is the Items[] indexing, as the text/value field properties require the name of the field they have to retrieve the value from.

Frans Bouma | Lead developer LLBLGen Pro
niti
User
Posts: 7
Joined: 28-Jun-2005
# Posted on: 28-Jun-2005 21:36:39   

Thank you, it worked.

niti
User
Posts: 7
Joined: 28-Jun-2005
# Posted on: 28-Jun-2005 22:36:59   

Well, thanks again for the prompt answer.

I have another one.

Before assigning the collection to the drop down, I want it sorted. I wrote the following code:

// Declare User collection: CollectionClasses.UserCollection all = new CollectionClasses.UserCollection(); // populate it: all.GetMulti(null); // now the sort: all.SupportsSorting = true; // line bellow does not work: //all.Sort((int)UserFieldIndex.UserId, System.ComponentModel.ListSortDirection.Ascending); // this does: all.Sort(1, System.ComponentModel.ListSortDirection.Ascending);

I converted UserFieldIndex.UserId to int because if I let it be like in the documentation ("How do I sort a filled entity collection?") I get this error Cannot convert UserFieldIndex to int. I noticed that the complier does not mention UserFieldIndex.UserId but only UserFieldIndex. The value of UserFieldIndex.UserId should be 1.

Now, both lines above compile without error, but only the second actually sorts.

What did I do wrong?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 29-Jun-2005 11:37:01   

niti wrote:

Well, thanks again for the prompt answer.

I have another one.

Before assigning the collection to the drop down, I want it sorted. I wrote the following code:

// Declare User collection: CollectionClasses.UserCollection all = new CollectionClasses.UserCollection(); // populate it: all.GetMulti(null); // now the sort: all.SupportsSorting = true; // line bellow does not work: //all.Sort((int)UserFieldIndex.UserId, System.ComponentModel.ListSortDirection.Ascending); // this does: all.Sort(1, System.ComponentModel.ListSortDirection.Ascending);

I converted UserFieldIndex.UserId to int because if I let it be like in the documentation ("How do I sort a filled entity collection?") I get this error Cannot convert UserFieldIndex to int. I noticed that the complier does not mention UserFieldIndex.UserId but only UserFieldIndex. The value of UserFieldIndex.UserId should be 1.

If it's the first field in the index, it's 0, not 1 simple_smile Could you check that for me please?

Thanks for reporting that error in the documentation, it indeed was bad code. I've updated it and it's fixed with the next build.

Frans Bouma | Lead developer LLBLGen Pro
niti
User
Posts: 7
Joined: 28-Jun-2005
# Posted on: 29-Jun-2005 14:29:56   

Thanks again.

You're right, userId is 0, but I was sorting by Name, which is 1.