C# 连接两个字段以显示在下拉列表中
我试图将列表中的两个字段连接起来,以显示在下拉列表中。 下面是我试图使用的代码。我不想改变我的产品的模型,所以我试着做下面这样的事情,但是如果不建立我自己的带有连接字段的对象,我什么都想不出来C# 连接两个字段以显示在下拉列表中,c#,drop-down-menu,C#,Drop Down Menu,我试图将列表中的两个字段连接起来,以显示在下拉列表中。 下面是我试图使用的代码。我不想改变我的产品的模型,所以我试着做下面这样的事情,但是如果不建立我自己的带有连接字段的对象,我什么都想不出来 skuDropDown.DataSource = List<product> skuDropDown.DataTextField = "ProductId" // want to combine with"Description";
skuDropDown.DataSource = List<product>
skuDropDown.DataTextField = "ProductId" // want to combine with"Description";
skuDropDown.DataValueField = "ProductId";
skuDropDown.DataBind();
skuDropDown.DataSource=List
skuDropDown.DataTextField=“ProductId”//要与“Description”结合使用;
skuDropDown.DataValueField=“ProductId”;
skuDropDown.DataBind();
谢谢,任何想法都会有所帮助。创建一个扩展产品的新类,将列表内容转换为扩展类,扩展类包含一个新属性,该属性返回ProductID和Description的串联值 我想那应该对我有用
我知道您不想更改产品结构,这就是我建议创建扩展类的原因。但是,如果不将其绑定到对象的字段,则无法执行此操作。不幸的是,如果使用数据绑定,则DataTextField必须是数据源上的字段名称 您可以做的一件事是在绑定dropdownlist中的项并修改它们的文本属性后,对它们进行迭代。唯一要做的另一件事是将连接字段添加到数据对象。您可以这样做:
List<Product>.ForEach(
x => skuDropDown.Items.Add(
new Item(x.ProductId + " " x.ProductDescription, x.ProductId)
);
List.ForEach(
x=>skuDropDown.Items.Add(
新项(x.ProductId+“”x.ProductDescription,x.ProductId)
);
只需在列表中循环并将每个项目添加到下拉列表中。这就是.net在您的示例中幕后为您所做的。若要使用给定的方法分配源,我将使用LINQ创建一个具有所需属性的匿名类型。类似于
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();
List products=newlist();
Add(新产品(){ProductId=1,Description=“Foo”});
products.Add(新产品(){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();
只需重写产品的.ToString()方法
public override string ToString()
{
return ProductID + " " + ProductDescription;
}
然后你需要做的就是绑定到下拉列表。据我所知,下拉列表标签ar绑定到它绑定到的集合中的对象的tostring()
换句话说,就是这样做
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();
List products=newlist();
Add(新产品(){ProductId=1,Description=“Foo”});
products.Add(新产品(){ProductId=2,Description=“Bar”});
var productQuery=products.Select(p=>new{ProductId=p.ProductId,DisplayText=p.ProductId.ToString()+“”+p.Description});
skuDropDown.DataSource=productQuery;
skuDropDown.DataBind();
如果您有一个类来表示产品,只需创建一个属性来扩展您的类并将其组合返回,例如:
public string ID_Description {
get
{
return string.Format("{0} ({1})", Name, ProductId);
}
}
并且,在数据绑定下拉列表中引用您的属性
skuDropDown.DataSource = productQuery;
skuDropDown.DataValueField = "ProductId";
skuDropDown.DataTextField = "ID_Description";
skuDropDown.DataBind();
恐怕你别无选择,只能扩展域名