Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2016. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Program Name: EF Master / Detail Maintenance
- '
- ' Author: Joseph L. Bolen
- ' Date Created: 30 NOV 2016
- '
- ' Description: Demonstrate how to use the Entity Framework with Windows Forms
- ' in a Master / Detail maintenance program.
- '
- ' Documentation is at:
- ' App's screen image is at: http://imgur.com/XCggdZF
- ' App's Visual Basic .NET code is at: http://pastebin.com/F77mRDAW
- ' Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
- '
- ' Steps:
- ' 1. Create WinForm Project.
- ' 2. Add ObservableListSource class
- ' 3. Add ADO.NET Enitity Data Model for Entity Framework 6. Then
- ' Modify "...Model.tt"
- ' 4. Replace first two occurrences of ICollection with ObservableListSource
- ' 5. Replace first occurrence of HashSet with ObservableListSource
- ' 6. Save and build.
- '
- ' For additional information see ...
- ' "Entity Framework Databinding with WinForms"
- ' https://msdn.microsoft.com/en-us/library/jj682076(v=vs.113).aspx
- '
- ' "Entity Framework Databinding with WPF"
- ' https://msdn.microsoft.com/en-us/library/jj574514(v=vs.113).aspx
- '
- ' The navigation of the master to detail was modified to use
- ' ObservableListSource class instead of ICollection.
- '
- '-------------------------------------------------------------------------------------------
- Imports System.ComponentModel
- Imports System.Data.Entity
- Public Class MainForm
- ' Declare DbContext
- Private ctx As SampleDBEntities
- ' Do intial housekeeping activites and get the data before displaying the form.
- Private Sub MainForm_Load(sender As Object, e As EventArgs) _
- Handles MyBase.Load
- ' Make DataGridView ColumnHeader Bold
- OrdersDataGridView.ColumnHeadersDefaultCellStyle.Font =
- New Font(OrdersDataGridView.ColumnHeadersDefaultCellStyle.Font, FontStyle.Bold)
- LoadAndDisplay()
- End Sub
- Private Sub LoadAndDisplay()
- If ctx IsNot Nothing Then
- ctx.Dispose()
- End If
- ' Instantiate DbContext
- ctx = New SampleDBEntities
- ' Load master table into memory, the details table will be loaded by navigation properties
- ctx.Customers.Load
- ' Alternative uses of LINQ to sort or filter data presented.
- 'ctx.Customers.OrderBy(Function(c) c.CustomerName).Load
- 'ctx.Customers.Where(Function(c) c.CustomerName.StartsWith("T")).Load
- CustomerBindingSource.DataSource = ctx.Customers.Local
- CustomerBindingSource.MoveFirst()
- End Sub
- Private Sub CustomerBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) _
- Handles CustomerBindingNavigatorSaveItem.Click
- 'TODO: Add Try/Catch for Errors.
- If Validate() Then
- CustomerBindingSource.EndEdit()
- OrdersBindingSource.EndEdit()
- For Each order In ctx.Orders.Local.ToList
- If order.Customer Is Nothing Then
- ctx.Orders.Remove(order)
- End If
- Next
- ctx.SaveChanges()
- LoadAndDisplay()
- End If
- End Sub
- ' Commit the master table records changes, when enter datagridview.
- Private Sub OrdersDataGridView_Enter(sender As Object, e As EventArgs) _
- Handles OrdersDataGridView.Enter
- CustomerBindingSource.EndEdit()
- End Sub
- ' Upon closing out the form, dispose of the DbContext.
- Private Sub MainForm_Closing(sender As Object, e As CancelEventArgs) _
- Handles Me.Closing
- 'TODO: Add check for DBContext is dirty before exit.
- If ctx IsNot Nothing Then
- ctx.Dispose()
- End If
- End Sub
- End Class
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2016. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' CLass Name: ObservableListSouce
- '
- ' VBnet Author: Joseph L. Bolen
- ' Date Created: 30 NOV 2016
- '
- ' Description: Class allow for observability and sorting in WinForm databinding
- ' in programs.
- '
- ' Documentation is at:
- ' App's screen image is at: http://imgur.com/XCggdZF
- ' App's Visual Basic .NET code is at: http://pastebin.com/F77mRDAW
- ' Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
- '
- ' Steps:
- ' 1. Create WinForm Project.
- ' 2. Add ObservableListSource class (This class.)
- ' 3. Add ADO.NET Enitity Data Model for Entity Framework 6. Then
- ' Modify "...Model.tt"
- ' 4. Replace first two occurrences of ICollection with ObservableListSource
- ' 5. Replace first occurrence of HashSet with ObservableListSource
- ' 6. Save and build.
- '
- ' For additional information see ...
- ' "Entity Framework Databinding with WinForms"
- ' https://msdn.microsoft.com/en-us/library/jj682076(v=vs.113).aspx
- '
- '-------------------------------------------------------------------------------------------
- Imports System.Collections.ObjectModel
- Imports System.ComponentModel
- Imports System.Data.Entity
- Public Class ObservableListSource(Of T As Class)
- Inherits ObservableCollection(Of T)
- Implements IListSource
- Private _bindingList As IBindingList
- Private ReadOnly Property IListSource_ContainsListCollection() As Boolean Implements IListSource.ContainsListCollection
- Get
- Return False
- End Get
- End Property
- Private Function IListSource_GetList() As IList Implements IListSource.GetList
- If _bindingList IsNot Nothing Then
- Return _bindingList
- Else
- _bindingList = Me.ToBindingList
- Return _bindingList
- End If
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment