C# 在Asp.net下拉列表中添加重复键
我有一个场景,我想在下拉列表中添加重复的键。以下是现有代码-C# 在Asp.net下拉列表中添加重复键,c#,asp.net,C#,Asp.net,我有一个场景,我想在下拉列表中添加重复的键。以下是现有代码- currentVersionDDL.DataSource = DDLList.ToList(); currentVersionDDL.DataTextField = "Value"; currentVersionDDL.DataValueField = "Key"; 字典在哪里 Dictionary<int, string> DDLList = new Dictionary<int, string>()
currentVersionDDL.DataSource = DDLList.ToList();
currentVersionDDL.DataTextField = "Value";
currentVersionDDL.DataValueField = "Key";
字典在哪里
Dictionary<int, string> DDLList = new Dictionary<int, string>();
Dictionary ddlist=newdictionary();
根据新的要求,我将不得不在下拉列表中添加重复键,是否有任何方法可以做到这一点而不需要对现有代码进行太多更改。请建议。我尝试了
查找
,但不适合 必须更改数据源类型才能执行此操作,因为字典由于明显的原因不允许重复的键,因此无法确定与键关联的值。一种快速、有效的解决方案:
List<KeyValuePair<int, string>> items = DDLList.AsEnumerable().ToList();
items.Add(new KeyValuePair<int, string>(3, "Name 1");
items.Add(new KeyValuePair<int, string>(3, "Name 1");
currentVersionDDL.DataSource = items;
currentVersionDDL.DataTextField = "Value";
currentVersionDDL.DataValueField = "Key";
List items=DDLList.AsEnumerable().ToList();
添加(新的KeyValuePair(3,“名称1”);
添加(新的KeyValuePair(3,“名称1”);
CurrentVersionDl.DataSource=项目;
CurrentVersionDl.DataTextField=“值”;
CurrentVersionDl.DataValueField=“Key”;
字典不允许重复键,但列表允许。使用这个简单的解决方案,您甚至不必更改下拉组件绑定属性。为什么出于兴趣而希望这样做?