在 ASP.NET C# 中对哈希表数据进行排序

发布于 2024-08-13 03:49:13 字数 1127 浏览 6 评论 0原文

我正在将下拉列表与枚举绑定,我有以下枚举和绑定下拉列表的代码。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

青春有你 2024-08-20 03:49:14

那么,HashTable 并不是完成这项工作的工具。尝试使用 SortedList

请记住,哈希表支持非常快速的搜索,但它不保留任何顺序

A HashTable isn't the tool for the job then. Try a SortedList.

Remember, A HashTable supports very fast searching, however it does not keep any ordering

萝莉病 2024-08-20 03:49:14

实际上有一个名为 的专门集合OrderedDictionary 为您提供两种功能:排序和键控访问。

There is actually a specialized collection called OrderedDictionary that gives you both abilities: sorting and keyed access.

陪你到最终 2024-08-20 03:49:14

您可以做的一件事是在 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.

浪漫人生路 2024-08-20 03:49:14

只需使用 SortedList 或用 SortedList 替换 HashTable

Just use SortedList or replace HashTable with SortedList

唱一曲作罢 2024-08-20 03:49:13

如果您希望项目按照添加顺序排列,则不需要哈希表或 SortedList。 (无论如何,当您实际上希望它们按键排序顺序排列时,排序列表就可以了,但是如果您后来决定需要调整顺序,则会导致问题。)特别是,您不会尝试使用以下功能:按键查找值,因此据我所知,您根本不需要 IDictionary<,>

您只需要一个包含键和值的类型的 List 。您可以使用匿名类型来做到这一点,例如:

var keyValuePairs = Enum.GetValues(enumDataSource)
                        .Select(x => new { key = x.ToString(), value = x })
                        .ToList();
BindDropDown(dropDownList, keyValuePairs, "key", "value");

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 an IDictionary<,> 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:

var keyValuePairs = Enum.GetValues(enumDataSource)
                        .Select(x => new { key = x.ToString(), value = x })
                        .ToList();
BindDropDown(dropDownList, keyValuePairs, "key", "value");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文