什么是排序数据绑定组合框的最佳方法? [英] What is the best way to sort a data bound combo box?

查看:126
本文介绍了什么是排序数据绑定组合框的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经对此做了一些研究,似乎排序数据绑定组合框的唯一方法是对数据源本身(在这种情况下是DataSet中的DataTable)进行排序。



如果是这样,那么问题是什么是排序DataTable的最佳方法?



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



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



我已经尝试设置

  this.typedDataSet.Table1.DefaultView.Sort =ColumnB DESC; 


但这没有什么区别,我已经尝试在控件构造函数中设置这个,在typedDataSet.Merge调用之前和之后。

解决方案

如果你使用DataTable,您可以使用(DataTable.DefaultView) DataView .Sort 财产。为了更大的灵活性,您可以使用 BindingSource 零件。 BindingSource将是您的组合框的DataSource。然后,您可以将数据源从DataTable更改为列表,而无需更改组合框的DataSource。


BindingSource组件提供
许多目的首先,它通过
简化
对数据的绑定控件,通过
提供货币管理,更改
通知以及Windows窗体控件和
数据源之间的其他服务



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).

If that is the case then the question becomes what is the best way to sort a 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";

But that makes no difference, I have tried setting this in the control constructor, before and after a typedDataSet.Merge call.

解决方案

If you're using a DataTable, you can use the (DataTable.DefaultView) DataView.Sort property. For greater flexibility you can use the BindingSource component. 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.

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.

这篇关于什么是排序数据绑定组合框的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆