如何在C#中对ResourceSet进行排序

时间:2009-11-09 10:34:51

标签: c# resources asp.net-3.5

我有一个名为filetypes.resx的资源文件。一些我如何计算出将资源值绑定到下拉列表,但我不知道如何对 ResourceSet 的值进行排序。

这是我到目前为止所做的,

FileTypes.resx

  • 名称,值
  • A,1

  • B,2

  • C,3

绑定下拉列表的代码


DropDownList1.DataSource = Resources.FileTypes.ResourceManager.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, true, true);
DropDownList1.DataTextField = "Key";
DropDownList1.DataValueField = "Value";
DropDownList1.DataBind();

结果

 A
 C
 B
As you can see the result is not sorted. Please help me to solve this issue.

非常感谢提前:)

4 个答案:

答案 0 :(得分:6)

ressource集有一个IDictionaryEnumerator,所以我猜它的项目是DictionaryEntry类型,尝试按如下方式对数据源进行排序:

DropDownList1.DataSource = Resources.FileTypes.ResourceManager
    .GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, true, true)
    .OfType<DictionaryEntry>()
    .OrderBy(i => i.Value);
嗯,我没有打开视觉工作室,所以不要责怪我万一它不能立即工作:)

答案 1 :(得分:1)

ResourceSeta property named Table,类型为HashTable。也许您可以从该表生成一个排序列表(手动或使用LINQ),然后将此列表分配给DropDownList1.DataSource

答案 2 :(得分:0)

Hashtable不是已排序的集合。

答案 3 :(得分:0)


Hashtable tbl = Resources.FileTypes.ResourceManager.
GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, true, true).Table;
List source = new List(tbl.Values);
source.Sort();
DropDownList1.DataSource = source;
DropDownList1.DataBind();