将哈希表绑定到下拉列表时是否可以对哈希表进行排序 [英] is it possible to sort hash table while binding it to dropdown

查看:66
本文介绍了将哈希表绑定到下拉列表时是否可以对哈希表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下所示的哈希表...

i have hash table like below...

public Hashtable BindDD()
    {
        Hashtable objDd = new Hashtable();
        objDd.Add("0", "DD");
        objDd.Add("1", "1");
        objDd.Add("2", "2");
        objDd.Add("3", "3");
        objDd.Add("4", "4");
        objDd.Add("5", "5");
        objDd.Add("6", "6");
        objDd.Add("7", "7");
        objDd.Add("8", "8");
        objDd.Add("9", "9");
        objDd.Add("10", "10");
        objDd.Add("11", "11");
        objDd.Add("12", "12");
        objDd.Add("13", "13");
        objDd.Add("14", "14");
        objDd.Add("15", "15");
        objDd.Add("16", "16");
        objDd.Add("17", "17");
        objDd.Add("18", "18");
        objDd.Add("19", "19");
        objDd.Add("20", "20");
        objDd.Add("21", "21");
        objDd.Add("22", "22");
        objDd.Add("23", "23");
        objDd.Add("24", "24");
        objDd.Add("25", "25");
        objDd.Add("26", "26");
        objDd.Add("27", "27");
        objDd.Add("28", "28");
        objDd.Add("29", "29");
        objDd.Add("30", "30");
        objDd.Add("31", "31");
        return objDd;
    }


我将此返回值绑定到下拉列表


i am binding this return value to dropdown

public void BindDD()
   {
       Hashtable ht = (new GlobalConstants().BindDD());
       ddlDD.DataSource = ht;
       ddlDD.DataTextField = "Value";
       ddlDD.DataValueField = "Key";
       ddlDD.DataBind();
   }



这是我绑定到下拉列表时无法解决的问题,因为我无法将其添加到哈希表中.是否可以对哈希表进行排序.. Plz帮助我解决了这个问题..

在此先感谢..



Here is my Problem when i am binding to drop down i am not able bind in order..in which i added to hash table. is it possible to sort the hash table .. Plz help me in solving this problem ..

Thanks in advance..

推荐答案

如果要排序,就使用了错误的集合. HashTables并不是可排序的. List< t>是.因此,为什么不做一个小的类或结构(取决于要求)并创建一个List< t>呢?这种类型的.然后,您可以根据自己的喜好对它进行排序,甚至可以提供自己的自定义排序.
If you want sortable, you''re using the wrong collection. HashTables are not meant to be sortable. List<t> is. So why not make a small class or structure (depending on requirements) and create a List<t> of that type. Then you can sort it to your hearts content and even provide your own custom sort.


您不能对HashTable进行排序.如果可以的话,它就不是HashTable.您可以枚举HashTable,然后对枚举排序.但这将非常缓慢.建议使用.NET SortedDictionary.
You can''t sort a HashTable. If you could, it wouldn''t be a HashTable. You can enumerate the HashTable, and then sort the enumeration. But that would be very slow. Recommendation would be to use .NET SortedDictionary instead.


使用字典而不是哈希表

Use Dictionary rather than Hash Table

Dictionary<int, string> myDict = new Dictionary<int, string>();
myDict.Add(2, "This");
myDict.Add(1, "is");
myDict.Add(5, "radio");
myDict.Add(4, "clash");

List<string> song = new List<string>(myDict.Values);
song.Sort();

http://www.c-sharpcorner.com/uploadfile /prasoonk/hashtable-sorting/ [ ^ ]


这篇关于将哈希表绑定到下拉列表时是否可以对哈希表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆