【问题标题】:Sorted Combobox排序组合框
【发布时间】:2012-07-19 12:30:18
【问题描述】:

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

【问题讨论】:

  • 我的绑定数据源可以是任何东西的 List,因为这个控件会从不同的地方用不同的数据调用。我尝试实现 OnDataSourceChanged() 方法并使用反射和显示字段对数据源进行排序,但由于我真的不知道数据源是什么,所以我没有设法找到通用方法。

标签: winforms sorting data-binding combobox


【解决方案1】:

数据绑定的组合框不能直接排序。您必须对基础数据源进行排序。这是来自 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 作为绑定源。

【讨论】:

    【解决方案2】:

    尝试使用它,它对我来说很好用。只更改控件的名称

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

    【讨论】:

      猜你喜欢
      • 2013-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多