Concatenate Two Fields to Display in Dropdown List
I am trying to concatenate two fields from a list to display in a dropdown. Below is the code i am trying to use. I don't want to change the model of my products so I was trying to do something like that below but I can't figure anything out without building out my own object with the fields concatenated.
开发者_运维问答 skuDropDown.DataSource = List<product>
skuDropDown.DataTextField = "ProductId" // want to combine with"Description";
skuDropDown.DataValueField = "ProductId";
skuDropDown.DataBind();
Thanks any ideas will help.
To assign the source with your given method, I would go for using LINQ to create an anonymous type with the properties you want. Something like
List<Product> products = new List<Product>();
products.Add(new Product() { ProductId = 1, Description = "Foo" });
products.Add(new Product() { ProductId = 2, Description = "Bar" });
var productQuery = products.Select(p => new { ProductId = p.ProductId, DisplayText = p.ProductId.ToString() + " " + p.Description });
skuDropDown.DataSource = productQuery;
skuDropDown.DataValueField = "ProductId";
skuDropDown.DataTextField = "DisplayText";
skuDropDown.DataBind();
IF you have a class to represent a product, just create a property that extend your class and return it combined, for example:
public string ID_Description {
get
{
return string.Format("{0} ({1})", Name, ProductId);
}
}
and, in your databind dropdown reference your property
skuDropDown.DataSource = productQuery;
skuDropDown.DataValueField = "ProductId";
skuDropDown.DataTextField = "ID_Description";
skuDropDown.DataBind();
You can do this:
List<Product>.ForEach(
x => skuDropDown.Items.Add(
new Item(x.ProductId + " " x.ProductDescription, x.ProductId)
);
Just loop through the list and add each item to the drop down list. It's what .net will do for you behind the scenes in your example.
Create a new class which extends the product, cast the List contents as the extended class, which contains a new property, which returns the concatenated values of ProductID and Description.
I think that should work OTOMH.
I know that you don't want to change the products structure, which is why I suggested making an extended class. But afaik it's not possible without binding it to a field of the object.
Unfortunately, if you're using databinding, the DataTextField must be the name of a field on your data source.
One thing you can do is iterate the items in the dropdownlist after you've bound them and modify their Text properties. The only other thing to do is get the concatenated field added to the data object.
All you need to do is override the .ToString() method of the Product.
public override string ToString()
{
return ProductID + " " + ProductDescription;
}
Then all you need to do is bind to the drop down. From my understanding, the dropdown lables ar bound to the tostring() of the objects in the collection it's bound to.
in other words, do this.
List<Product> products = new List<Product>();
products.Add(new Product() { ProductId = 1, Description = "Foo" });
products.Add(new Product() { ProductId = 2, Description = "Bar" });
var productQuery = products.Select(p => new { ProductId = p.ProductId, DisplayText = p.ProductId.ToString() + " " + p.Description });
skuDropDown.DataSource = productQuery;
skuDropDown.DataBind();
精彩评论