C# 对数据绑定组合框进行排序的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 08:21:18  来源:igfitidea点击:

What is the best way to sort a data bound combo box?

提问by John Hunter

I have done a bit of research into this and it seems that the only way to sort a data bound combo box is to sort the data source itself (a DataTable in a DataSet in this case).

我对此进行了一些研究,似乎对数据绑定组合框进行排序的唯一方法是对数据源本身进行排序(在本例中为 DataSet 中的 DataTable)。

If that is the case then the question becomes what is the best way to sort a DataTable?

如果是这种情况,那么问题就变成了对 DataTable 进行排序的最佳方法是什么?

The combo box bindings are set in the designer initialize using

组合框绑定在设计器初始化中使用

myCombo.DataSource = this.typedDataSet;
myCombo.DataMember = "Table1";
myCombo.DisplayMember = "ColumnB";
myCombo.ValueMember = "ColumnA";

I have tried setting

我试过设置

this.typedDataSet.Table1.DefaultView.Sort = "ColumnB DESC";
但这没有区别,我尝试在 typedDataSet.Merge 调用之前和之后在控件构造函数中设置它。

采纳答案by jfs

If you're using a DataTable, you can use the (DataTable.DefaultView) DataView.Sortproperty. For greater flexibility you can use the BindingSourcecomponent. BindingSource will be the DataSource of your combobox. Then you can change your data source from a DataTable to List without changing the DataSource of the combobox.

如果您使用的是 DataTable,则可以使用 (DataTable.DefaultView) DataView.Sort属性。为了获得更大的灵活性,您可以使用BindingSource组件。BindingSource 将是您的组合框的数据源。然后,您可以将数据源从 DataTable 更改为 List,而无需更改组合框的 DataSource。

The BindingSource component serves many purposes. First, it simplifies binding controls on a form to data by providing currency management, change notification, and other services between Windows Forms controls and data sources.

BindingSource 组件有多种用途。首先,它通过在 Windows 窗体控件和数据源之间提供货币管理、更改通知和其他服务,简化了将窗体上的控件绑定到数据。

回答by Andy Rose

Does the data need to be in a DataTable? Using a SortedList and binding that to a combo box would be a simpler way.

数据是否需要在 DataTable 中?使用 SortedList 并将其绑定到组合框将是一种更简单的方法。

If you need to use a DataTable you can use the Select method to retrieve a DataView and pass in a sort parameter.

如果需要使用 DataTable,可以使用 Select 方法检索 DataView 并传入排序参数。

DataView dv = myDataTable.Select("filter expression", "sort");

回答by Matt Hamilton

You can actually sort the default viewon a DataTable:

您实际上可以对 DataTable 上的默认视图进行排序:

myDataTable.DefaultView.Sort = "Field1, Field2 DESC";

That'll sort any rows you retrieve directly from the DataTable.

这将对您直接从 DataTable 检索的任何行进行排序。

回答by urini

The simplest way to sort a ComboBox is to use the ComboBox.Sortedproperty. However, that won't work if you're using data binding. In that case you'll have to sort the data source itself.

对 ComboBox 进行排序的最简单方法是使用ComboBox.Sorted属性。但是,如果您使用数据绑定,这将不起作用。在这种情况下,您必须对数据源本身进行排序。

You can use either a SortedListor SortedDictionary(both sort by the Key), or a DataView.

您可以使用SortedListSortedDictionary(均按键排序)或DataView

The DataView has a Sortproperty that accepts a sort expression (string) for example:

DataView 有一个Sort属性,它接受一个排序表达式(字符串),例如:

view.Sort = "State, ZipCode DESC";

In the above example both State and ZipCode are columns in the DataTable used to create the DataView.

在上面的示例中,State 和 ZipCode 都是用于创建 DataView 的 DataTable 中的列。

回答by Andy Rose

Make sure you bind the DefaultView to the Controls Datasource, after you set the Sort property, and not the table:

确保在设置 Sort 属性后将 DefaultView 绑定到控件数据源,而不是表:

myCombo.DataSource = this.typedDataSet.Tables["Table1"].DefaultView;
myCombo.DisplayMember = "ColumnB";
myCombo.ValueMember = "ColumnA";

回答by Adam Robinson

I realize that you've already chosen your answer to this question, but I would have suggested placing a DataView on your form, binding it to your DataSet/DataTable, and setting the sort on the View in the designer. You then bind your combobox to the DataView, rather than the DataSet/DataTable.

我意识到您已经选择了这个问题的答案,但我建议在您的表单上放置一个 DataView,将其绑定到您的 DataSet/DataTable,并在设计器中的 View 上设置排序。然后将组合框绑定到 DataView,而不是 DataSet/DataTable。

回答by David Veeneman

Josh Smith has a blog postthat answers this question, and does it all in XAML.

Josh Smith 有一篇博客文章回答了这个问题,并在 XAML 中完成了这一切。