C# 如何将datagridview数据源转换为BindingListView(Equin.ApplicationFramework.BindingListView)
我将此DataBindingComplete事件设置为我的datagridview。我希望绑定到datagridview的每个数据源都可以通过单击列进行排序C# 如何将datagridview数据源转换为BindingListView(Equin.ApplicationFramework.BindingListView),c#,datasource,C#,Datasource,我将此DataBindingComplete事件设置为我的datagridview。我希望绑定到datagridview的每个数据源都可以通过单击列进行排序 void MakeColumnsSortable_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { DataGridView dataGridView = sender as DataGridView;
void MakeColumnsSortable_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
DataGridView dataGridView = sender as DataGridView;
foreach (DataGridViewColumn column in dataGridView.Columns)
column.SortMode = DataGridViewColumnSortMode.Automatic;
}
我的所有数据源都是列表,当我的列表以.ToList结尾时,bindingsource不会排序
现在。如何将datagridview.datasource转换为Equin.ApplicationFramework.BindingListView,并将其再次设置为datasource,以使任何datagridview都可排序?Equin.ApplicationFramework.BindingListView的正确用法如下所示 创建表单时:
- 创建BindingListView。稍后将用您要显示/排序/筛选的项目填充它
- 创建绑定源
- 创建DataGridView
- 将列添加到要显示的属性中
InitializeComponents
中
假设您想显示/排序/过滤MyType
的元素,您的表单如下所示:
public class MyForm : Form
{
private BindingListView<MyType> MyItems {get; set;}
public MyForm()
{
InitializeComponent();
this.MyItems = new BindingListView<MyType>(this.components);
// components is created in InitializeComponents
this.MyBindingSource.DataSource = this.MyItems;
this.MyDataGridView.DataSource = this.MyBindingSource;
// assigning the DataPropertyNames of the columns can be done in the designer,
// however doing it the following way helps you to detect errors at compile time
// instead of at run time
this.columnPropertyA = nameof(MyType.PropertyA);
this.columnPropertyB = nameof(MyType.PropertyB);
...
}
就这些。您不需要创建特殊的函数来对鼠标单击进行排序。排序将自动完成,包括确定排序顺序和显示正确的排序图示符。如果要以编程方式排序,请执行以下操作:
// sort columnPropertyA in descending order:
this.MyDataGridView.Sort(this.columnPropertyA.ListsortDirection.Descending);
BindingListView
的一个优点是过滤选项:
// show only items where PropertyA not null:
this.MyItems.ApplyFilter(myItem => myItem.PropertyA != null);
// remove the filter:
this.MyItems.RemoveFilter();
(我不确定在应用或删除筛选器后是否需要刷新()正确使用Equin.ApplicationFramework.BindingListView如下所示 创建表单时:
- 创建BindingListView。稍后将使用要显示/排序/筛选的项目填充该视图
- 创建绑定源
- 创建DataGridView
- 将列添加到要显示的属性中
InitializeComponents
中
假设您想显示/排序/过滤MyType
的元素,您的表单如下所示:
public class MyForm : Form
{
private BindingListView<MyType> MyItems {get; set;}
public MyForm()
{
InitializeComponent();
this.MyItems = new BindingListView<MyType>(this.components);
// components is created in InitializeComponents
this.MyBindingSource.DataSource = this.MyItems;
this.MyDataGridView.DataSource = this.MyBindingSource;
// assigning the DataPropertyNames of the columns can be done in the designer,
// however doing it the following way helps you to detect errors at compile time
// instead of at run time
this.columnPropertyA = nameof(MyType.PropertyA);
this.columnPropertyB = nameof(MyType.PropertyB);
...
}
仅此而已。无需创建特殊函数即可在鼠标单击时进行排序。排序将自动完成,包括确定排序顺序和显示正确的排序图示符。如果要以编程方式排序,请执行以下操作:
// sort columnPropertyA in descending order:
this.MyDataGridView.Sort(this.columnPropertyA.ListsortDirection.Descending);
BindingListView
的一个优点是过滤选项:
// show only items where PropertyA not null:
this.MyItems.ApplyFilter(myItem => myItem.PropertyA != null);
// remove the filter:
this.MyItems.RemoveFilter();
(我不确定在应用或删除筛选器后是否需要刷新()。什么是
?。Object
-为我抛出语法错误。在没有?
MyObject?的情况下工作。Myproperty使用空条件运算符(在C#6中新增)请参阅,我使用了null条件运算符,因为如果未选择任何内容,则MyBindingSource、Current返回null,在这种情况下,我需要一个返回null的对象。或者,您可以在访问属性MyBindingSource.Current.ObjectWhat is?之前检查MyBindingSource.Current是否不等于null。object
-引发语法错误对于我来说。在没有?
MyObject?的情况下工作。Myproperty使用空条件运算符(在C#6中是新的?)请参阅,我使用了null条件运算符,因为如果未选择任何内容,则MyBindingSource、Current将返回null,在这种情况下,我希望返回一个null对象。或者,您可以在访问属性MyBindingSource.Current.object之前检查MyBindingSource.Current是否不等于null