在Bindinglist上过滤 [英] filtering on a Bindinglist
问题描述
你好!
我正在使用vb.net 2017和实体框架6
I'm using vb.net 2017 and entity Framework 6
我有一个学生桌,我也是有一个在设计中配置的Bindingosurce, 绑定到此绑定源的datagridview。
I have a Students Table , also I have a Bindingosurce that is configured in design, and a datagridview bound to this bindingsource.
我想首先加载所有学生,并在使用本地数据后使用我需要的数据填充bindingsource:
I want to first load all students , and after work with local data to fill the bindingsource with the data that I needs :
我有这段代码:
Context.Myobjs1.Students.Load()
Mybindingsource1.Datasource=Context.obj1s.Local.TobindingList
如果我使用此代码一切正常,我可以在gridview上显示数据,我可以在gridview上添加,删除或修改数据并保存它们。
If I use this code everything works ok , I can display data on gridview , I can add , delete or modify data on gridview and save them.
但问题是,根据用户的选择,我想在bindingsource上过滤数据,所以我不想要在我的第二行代码上使用的所有本地数据,但是例如我想要所有具有nr> 1的研究。
But the problems is that depending on user choice , I want to filter data on bindingsource , so I don't want all the local data that is used on my second line of code ,but for example I want all studnets that have nr>1.
但是如果我使用:
Mybindingsource1.Datasource=Context.obj1s.Local.TobindingList.Where(Function(t2) t2.nr>1).Tolist
数据在datagridview中正确显示,但是当我添加或删除时某些东西,更改不会保存。
The data is displayed correctly in datagridview , but when I add or delete something , the changes are not saved .
如果我尝试使用:
Mybindingsource1.Datasource=Context.obj1s.Local.Where(Function(t2) t2.nr>1).TobindingList
在这种情况下没有tobindingList。
There's no tobindingList in this case.
我知道我可以在从数据库加载时过滤数据,但事实并非如此,因为我想从数据库加载所有数据,之后我想使用本地数据。
I know that I can filter the data when loading from database , but this is not the case because I want to load all data from database , and after I want to work with local data.
那我该怎么办?
谢谢!
推荐答案
您好dcode25,
Hi dcode25,
以下主题提供了有关如何添加/删除/编辑的示例代码/通过BindingSource过滤以供参考。
The following thread provide a sample code about how to Add/Remove/Edit/Filter via BindingSource for your reference.
#Filter related。
#Filter related.
Private Sub FilterButton_Click(ByVal sender As Object, ByVal e As EventArgs)
If String.IsNullOrEmpty(Me.FilterTextBox.Text) Then
Me.Mybindingsource1.DataSource = db.obj1s.Local.ToBindingList()
Else
Dim filteredData = db.obj1s.Local.Where(Function(t2) t2.nr>1)
Me.Mybindingsource1.DataSource = filteredData
End If
End Sub
此外,您还可以参考以下主题,该主题使用 BindingListView
In addition, you could also refer to the following thread, which use BindingListView
https://stackoverflow.com/questions/23309112/ef-in-winforms-how-to-filter-data-in-bindingsource-dgw-loc al-todingdinglist
祝你好运,
张龙
这篇关于在Bindinglist上过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!