数据绑定组合框中不同的值 [英] Distinct values in data bound combobox
问题描述
我有一个表,存货(ITEMID,名称,规格,价格,活动促销),其中ITEMID是主键,名称,规格,价格是唯一的。结果
当我绑定组合框与名称重复的所有名称出现,而我想每个名字出现只有一次,也会有相同的尺寸。
I have a table Inventory(ItemId,Name,Size,Price,otherinfo) where ItemId is primary key and Name,Size,Price are unique.
When I bind the combobox with Name all repeated names appear while i want each name to appear just once, same happens with Size.
如何加载它绑定到数据源在组合框中独特价值?
推荐答案
您可以这样做,(你可能不得不调整它有点complie并为你的工作)
You can do this, (you may have to tweak it a bit to complie and work for you)
ddlName.DataSource = items.Select(item=>item.Name).Distinct().ToList();
ddlName.DataBind();
ddlSize.DataSource = items.Select(item=>item.Size).Distinct().ToList();
ddlSize.DataBind();
ddlPrice.DataSource = items.Select(item=>item.Price).Distinct().ToList();
ddlPrice.DataBind();
,然后找到基于三个下拉列表中选择条目标识。
And then find the itemID based on the selection of all three dropdown lists.
这是C#和假定你有LINQ
This is C# and assumes that you have LINQ
希望这有助于。
Edit--(如果没有LINQ)
Edit-- (if no LINQ)
IList<string> names = new List<string>();
foreach (Item item in Items)
if (!names.Contains(item.Name))
names.Add(name);
ddlName.DataSource = names;
ddlName.DataBind();
//Do similar for price and size.
编辑(使用SQL命令)
Edit (use SQL commands)
select distinct Name from Item
select distinct Size from Item
select distinct Price from Item
这篇关于数据绑定组合框中不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!