Winforms 排序组合框

Winforms 排序组合框,winforms,sorting,data-binding,combobox,Winforms,Sorting,Data Binding,Combobox,我使用带有combobx的数据绑定在下拉列表中显示我的对象。组合框需要在数据绑定后按字母顺序自动对其中的数据进行排序。如何做到这一点?我希望逻辑是通用的,直接应用于组合框,而不是绑定到它的对象。数据绑定组合框不能直接排序。您必须对底层数据源进行排序。这来自MSDN: Attempting to set the Sorted property on a data-bound control raises an ArgumentException. You must sort the data us

我使用带有combobx的数据绑定在下拉列表中显示我的对象。组合框需要在数据绑定后按字母顺序自动对其中的数据进行排序。如何做到这一点?我希望逻辑是通用的,直接应用于组合框,而不是绑定到它的对象。

数据绑定组合框不能直接排序。您必须对底层数据源进行排序。这来自MSDN:

Attempting to set the Sorted property on a data-bound control raises an
ArgumentException. You must sort the data using the underlying data model.

因此,您可能可以使用SortedList作为绑定源。

尝试使用它,它与我配合良好。仅更改控件的名称

private void sellingTableDataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
     if (sellingTableDataGridView.CurrentCell.ColumnIndex == 5) {
         mainItemsDataBindingSource.Sort = "ItemCodeID";
     }
}

我的绑定数据源可以是任何内容的列表,因为该控件将从具有不同数据的不同位置调用。我尝试实现OnDataSourceChanged()方法,并使用反射和显示字段对数据源进行排序,但由于我不知道数据源是什么,因此没有找到一种通用的方法。