Scott Baldridge
asked on
Help with IEnumerable Collection
Hello, I need some help understanding how to accomplish this task:
Given a class called Product with a shared method GetProducts() that returns an IEnumerable collection of Product objects with the properties ID and Desc, how would you populate a drop down list?
Given a class called Product with a shared method GetProducts() that returns an IEnumerable collection of Product objects with the properties ID and Desc, how would you populate a drop down list?
ASKER
Thank you for the explanation! So much to learn and I'm new to C#.
Would something like this work?
myProducts IList = Products.GetPlatforms();
ddlProducts.TextField = "Desc;
ddlProducts.ValueField = "ID";
ddlProducts.DataSource = myProducts;
ddlProducts.DataBind();
Would something like this work?
myProducts IList = Products.GetPlatforms();
ddlProducts.TextField = "Desc;
ddlProducts.ValueField = "ID";
ddlProducts.DataSource = myProducts;
ddlProducts.DataBind();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for your time and help explaining. I really appreciate your help! I need to read some more.
.NET collections also implement IList, which is what you are usually after when you want to fill a list of a grid with data.
Find what you have hidden under your IEnumerable collection, and use that type instead of IEnumerable. You can see what is hiding there by hitting a breakpoint when there is something in the collection. Examine the collection in the debugger, and you will have its type somewhere in there.
Using the collection by its real type instead of through one of its interfaces, you simply fill your dropdown the following way, assuming that yu are in a Windows Application. There is something similar but maybe somewhat different for Web applications.
yourDropDown.DataSource = yourCollectionObject; // This fills your dropdown with the collection
yourDropDown.DisplayMember
yourDropDown.DisplayValue=
Once the user has made a selection, you can use yourDropDown.Text to retrieve the selected description, and yourDropDown.SelectedValue