在 ASP.NET C# 中对哈希表数据进行排序
我正在将下拉列表与枚举绑定,我有以下枚举和绑定下拉列表的代码。
public enum DignosisOrderType
{
All = 0,
General = 1,
Uveitis = 2,
Coag =3,
PreOp=4,
Tests=5,
RP =6
}
public static void BindDropDownByEnum(DropDownList dropDownList, Type enumDataSource, )
{
Hashtable htDataSource = new Hashtable();
string[] names = Enum.GetNames(enumDataSource);
Array values = Enum.GetValues(enumDataSource);
for (int i = 0; i < names.Length; i++)
htDataSource.Add(names[i], values.GetValue(i));
BindDropDown(dropDownList, htDataSource, "key", "value");
}
public static void BindDropDown(DropDownList dropDownList, object dataSource, string dataTextField, string dataValueField)
{
dropDownList.DataSource = dataSource;
dropDownList.DataTextField = dataTextField;
dropDownList.DataValueField = dataValueField;
dropDownList.DataBind();
}
当 Dropdownlist 绑定时,数据不按排序顺序传送,我希望 Dropdownlist 按创建 Enum 的顺序绑定。
I am binding my dropdownlist with Enum I have following enum and code for bind dropdownlist.
public enum DignosisOrderType
{
All = 0,
General = 1,
Uveitis = 2,
Coag =3,
PreOp=4,
Tests=5,
RP =6
}
public static void BindDropDownByEnum(DropDownList dropDownList, Type enumDataSource, )
{
Hashtable htDataSource = new Hashtable();
string[] names = Enum.GetNames(enumDataSource);
Array values = Enum.GetValues(enumDataSource);
for (int i = 0; i < names.Length; i++)
htDataSource.Add(names[i], values.GetValue(i));
BindDropDown(dropDownList, htDataSource, "key", "value");
}
public static void BindDropDown(DropDownList dropDownList, object dataSource, string dataTextField, string dataValueField)
{
dropDownList.DataSource = dataSource;
dropDownList.DataTextField = dataTextField;
dropDownList.DataValueField = dataValueField;
dropDownList.DataBind();
}
when the Dropdownlist is bind the data is not comming in sorting order,I want dropdownlist is bind in order of Enum is created.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
那么,
HashTable
并不是完成这项工作的工具。尝试使用SortedList
。请记住,哈希表支持非常快速的搜索,但它不保留任何顺序
A
HashTable
isn't the tool for the job then. Try aSortedList
.Remember, A HashTable supports very fast searching, however it does not keep any ordering
实际上有一个名为 的专门集合OrderedDictionary 为您提供两种功能:排序和键控访问。
There is actually a specialized collection called OrderedDictionary that gives you both abilities: sorting and keyed access.
您可以做的一件事是在 BindDropDown 方法中迭代 HT,一次添加一个 ListItem,这样它们将按索引排序。
One thing you could do is iterate through the HT in the BindDropDown method adding one ListItem at a time, so they would be ordered by index.
只需使用 SortedList 或用 SortedList 替换 HashTable
Just use SortedList or replace HashTable with SortedList
如果您希望项目按照添加顺序排列,则不需要哈希表或
SortedList
。 (无论如何,当您实际上希望它们按键排序顺序排列时,排序列表就可以了,但是如果您后来决定需要调整顺序,则会导致问题。)特别是,您不会尝试使用以下功能:按键查找值,因此据我所知,您根本不需要IDictionary<,>
。您只需要一个包含键和值的类型的
List
。您可以使用匿名类型来做到这一点,例如:If you want the items in the order you're adding them, you don't want a hashtable or a
SortedList
. (A sorted list will be fine while you actually want them in the key sort order anyway, but if you later decide you need to tweak the order, it will cause problems.) In particular, you're not trying to use the ability to look up a value by key, so you don't need anIDictionary<,>
at all as far as I can see.You just want a
List<T>
for a type containing the key and value. You could do that with an anonymous type, for instance: