English 中文(简体)
如何分类ComboBox。 重要议题:
原标题:How to sort a ComboBox.Items collection of KeyValuePair<string,string> by Value?
  • 时间:2009-10-05 14:31:04
  •  标签:

页: 1

www.un.org/Depts/DGACM/index_spanish.htm 我怎么能以价值利用钥匙ValuePair,以便在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;

}
最佳回答

如果你从服务中受益,我就认为他们被列入清单或某种类别?


If you are using a list of items, you can user the LINQ Extension Method .OrderBy() to sort the list:
var myNewList = myOldList.OrderBy(i => i.Value);


If you are getting the data as a DataTable, you can set the default view of the table like this:
myTable.DefaultView.Sort = "Value ASC";
问题回答

页: 1 项目Control(例如ComboBox,ListB......), 您可使用对等操作进行管理。 ViewInterface。 基本上,你利用<代码>检索有关案件。 班级:

var collectionView = CollectionViewSource.GetDefaultView(this.collections);

之后,你可以采用“SortDescription”补充:

collectionView.SortDescriptions.Add(...)

假设从服务中回收的收集工作执行IEvidable<T>,那么你就应当能够做这样的工作:

Items.Add(new KeyValuePair<string, string>(null, "Please choose..."));
foreach (var item in collectionReturnedFromService.OrderBy(i => i.Value))
{
    Items.Add(item);
}

附有清单的预审:

List<KeyValuePair<string, string>> pairs =
        new List<KeyValuePair<string, string>>( /* whatever */ );

pairs.Sort(
    delegate(KeyValuePair<string, string> x, KeyValuePair<string, string> y)
    {
        return StringComparer.OrdinalIgnoreCase.Compare(x.Value, y.Value);
    }
);




相关问题
热门标签