I'm in the middle of refactoring a WinForms application that uses DataSets and DataTables. I am trying to move the data to ADO.NET Entities.
I'm at a point where I have my entities in my data sources window, and can drag them onto various DataGridViews
. The BindingSources
are automatically created. Then, in the code behind I set the data source:
Dim context = New Entities()
CompanyBindingSource.DataSource = context.companies.toList()
The problem is that a Linq to SQL query result is not a Sortable or Filterable binding source, so the sorting on the DGVs doesn't work; and I also can't set filters on the BindingSources
.
I came across this project and various other solutions which helps with some of this. They basically wrap the Linq query in a sortable list:
CompanyBindingSource.DataSource = BindingListView(Of company)(context.companies.toList())
and the DGV for companies works. But the problem is, the related entities (for example in my case, the list of company's contracts) are not wrapped in this way, and so their respective DGV's are still not sortable or filterable.
I want to make sure I'm not missing something before I go through and manually create all the binding sources and set their data source property. The drag-and-drop binding is super convenient and is used extensively throughout the project, so this would save a huge amount of time.
Is there anything else I'm missing?
The solution I came up with involves partial classes. Say I have a company
that has many contracts
. If I just dragged the relationship from the Data Sources panel, the contracts
wouldn't be sortable in my DGV. But I can just use partial classes for my company
and add my own collection:
Partial Public Class Company
Public ReadOnly Property visibleContracts As SortableBindingList(Of contract)
Get
Dim list = New SortableBindingList(Of contract)
For Each c As contract In contracts.ToList
list.Add(c)
Next
Return list
End Get
End Property
End Class
Now my DGV can bind to visibleContracts
, and the designer takes care of all the syncing up - but now the DGV is sortable!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.