【问题标题】:How to sort ResourceSet in C#如何在 C# 中对资源集进行排序
【发布时间】:2009-11-09 19:22:45
【问题描述】:

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

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

文件类型.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();

结果

一个 C 乙
As you can see the result is not sorted. Please help me to solve this issue.

提前非常感谢:)

【问题讨论】:

    标签: c# resources asp.net-3.5


    【解决方案1】:

    资源集有一个 IDictionaryEnumerator 所以我猜它的项目是 DictionaryEntry 类型,尝试像这样对数据源进行排序:

    DropDownList1.DataSource = Resources.FileTypes.ResourceManager
        .GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, true, true)
        .OfType<DictionaryEntry>()
        .OrderBy(i => i.Value);
    

    嗯,我没有打开视觉工作室,所以不要怪我,以防它不能立即工作:)

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        Hashtable 不是排序集合。

        【讨论】:

          【解决方案4】:
          
          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();
          
          

          【讨论】:

          • 虽然此代码可能会回答问题,但提供有关它为什么和/或如何回答问题的额外上下文将显着提高其长期价值。请编辑您的答案以添加一些解释。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-02-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多