gpt4 book ai didi

c# - 如何按值对 KeyValuePair 的 ComboBox.Items 集合进行排序?

转载 作者:太空狗 更新时间:2023-10-29 23:59:33 27 4
gpt4 key购买 nike

我从一个服务获取了一个 KeyValuePair,但一些值没有排序,如下所示。

如何按值对 KeyValuePair 进行排序,以便它们在 ComboBox 中按字母顺序显示:

public NationalityComboBox()
{
InitializeComponent();

Items.Add(new KeyValuePair<string, string>(null, "Please choose..."));
Items.Add(new KeyValuePair<string, string>("111", "American"));
Items.Add(new KeyValuePair<string, string>("777", "Zimbabwean"));
Items.Add(new KeyValuePair<string, string>("222", "Australian"));
Items.Add(new KeyValuePair<string, string>("333", "Belgian"));
Items.Add(new KeyValuePair<string, string>("444", "French"));
Items.Add(new KeyValuePair<string, string>("555", "German"));
Items.Add(new KeyValuePair<string, string>("666", "Georgian"));
SelectedIndex = 0;

}

最佳答案

如果您从服务中获取它们,我假设它们在列表或某种集合中?


如果您使用的是项目列表,则可以使用 LINQ 扩展方法 .OrderBy() 对列表进行排序:

var myNewList = myOldList.OrderBy(i => i.Value);


如果您将数据作为 DataTable 获取,则可以像这样设置表的默认 View :

myTable.DefaultView.Sort = "Value ASC";

关于c# - 如何按值对 KeyValuePair<string,string> 的 ComboBox.Items 集合进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1520457/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com