Mini Kabibi Habibi

Current Path : C:/Users/Public/Documents/DXperience 13.1 Demos/WPF/VB/GridDemo.Wpf/Controls/
Upload File :
Current File : C:/Users/Public/Documents/DXperience 13.1 Demos/WPF/VB/GridDemo.Wpf/Controls/MasterDetailItems.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Windows
Imports System.Windows.Data
Imports GridDemo
Imports DevExpress.Xpf.Editors.Settings
Imports DevExpress.Data.Mask
Imports DevExpress.Xpf.Editors
Imports DevExpress.Xpf.DemoBase.DataClasses
Imports DevExpress.Xpf.Grid
Imports System.Windows.Controls
Imports System.Linq
Imports System.Collections
Imports System.Collections.Generic
Imports System.Diagnostics
Imports DevExpress.Xpf.DemoBase.NWind
Imports System.Windows.Input
Imports DevExpress.Xpf.DemoBase
Imports DevExpress.Xpf.Utils
Imports System.Data

Namespace GridDemo

	Public Class OrdersWithDetail
		Inherits Orders
		Private privateInvoices As IList(Of Invoices)
		Public Property Invoices() As IList(Of Invoices)
			Get
				Return privateInvoices
			End Get
			Private Set(ByVal value As IList(Of Invoices))
				privateInvoices = value
			End Set
		End Property

		Public Shared Function CreateOrdersForMasterDetailView(ByVal customerID As String, ByVal employeeID As Integer) As IList(Of OrdersWithDetail)
			Dim orders As DataView = NWindData.Orders
			Dim res As IList(Of OrdersWithDetail) = New List(Of OrdersWithDetail)()
			Dim dict As Dictionary(Of Integer, Integer) = EmployeesWithPhotoData.OrdersRelationsDictionary
			For Each order As DataRowView In orders
				If (customerID = "" OrElse CStr(order("CustomerID")) = customerID) AndAlso (dict(CInt(Fix(order("OrderID"))))) = employeeID Then
					res.Add(New OrdersWithDetail(order))
				End If
			Next order
			Return res
		End Function
		Public Shared Function CreateOrders(ByVal customerID As String, ByVal employeeID As Integer) As IList(Of OrdersWithDetail)
			Dim orders As DataView = NWindData.Orders
			Dim res As IList(Of OrdersWithDetail) = New List(Of OrdersWithDetail)()
			Dim i As Integer = 0
			For Each order As DataRowView In orders
				If i > 10 Then
					Exit For
				End If
				If (customerID = "" OrElse CStr(order("CustomerID")) = customerID) AndAlso CInt(Fix(order("EmployeeID"))) = employeeID Then
					res.Add(New OrdersWithDetail(order))
					i += 1
				End If
			Next order
			Return res
		End Function
		Private Function CreateInvoices(ByVal orderID As Integer, ByVal customerID As String) As IList(Of Invoices)
			Dim invoices As DataView = NWindData.Invoices
			Dim res As IList(Of Invoices) = New List(Of Invoices)()
			For Each invoice As DataRowView In invoices
				If (customerID = "" OrElse CStr(invoice("CustomerID")) = customerID) AndAlso CInt(Fix(invoice("OrderID"))) = orderID Then
					res.Add(New Invoices() With {.Address = TryCast(invoice("Address"), String), .City = TryCast(invoice("City"), String), .Country = TryCast(invoice("Country"), String), .CustomerID = TryCast(invoice("CustomerID"), String), .Discount = Convert.ToDecimal(CSng(invoice("Discount"))), .ExtendedPrice = CDec(invoice("ExtendedPrice")), .Freight = CDec(invoice("Freight")), .OrderDate = CDate(invoice("OrderDate")), .OrderID = CInt(Fix(invoice("OrderID"))), .PostalCode = TryCast(invoice("PostalCode"), String), .ProductID = CInt(Fix(invoice("ProductID"))), .ProductName = TryCast(invoice("ProductName"), String), .Quantity = CShort(Fix(invoice("Quantity"))), .Region = TryCast(invoice("Region"), String), .RequiredDate = CDate(invoice("RequiredDate")), .Salesperson = TryCast(invoice("Salesperson"), String), .ShipAddress = TryCast(invoice("ShipAddress"), String), .ShipCity = TryCast(invoice("ShipCity"), String), .ShipCountry = CountryNameResolver.Resolve(TryCast(invoice("ShipCountry"), String)), .ShipName = TryCast(invoice("ShipName"), String), .ShipPostalCode = TryCast(invoice("ShipPostalCode"), String), .ShipRegion = TryCast(invoice("ShipRegion"), String), .UnitPrice = CDec(invoice("UnitPrice"))})
				End If
			Next invoice
			Return res
		End Function
		Public Sub New(ByVal o As DataRowView)
			Me.CustomerID = TryCast(o("CustomerID"), String)
			Me.EmployeeID = CInt(Fix(o("EmployeeID")))
			Me.Freight = CDec(o("Freight"))
			Me.OrderDate = CDate(o("OrderDate"))
			Me.OrderID = CInt(Fix(o("OrderID")))
			Me.RequiredDate = CDate(o("RequiredDate"))
			Me.ShipAddress = TryCast(o("ShipAddress"), String)
			Me.ShipCity = TryCast(o("ShipCity"), String)
			Me.ShipCountry = CountryNameResolver.Resolve(TryCast(o("ShipCountry"), String))
			Me.ShipName = TryCast(o("ShipName"), String)
			Me.ShipPostalCode = TryCast(o("ShipPostalCode"), String)
			Me.ShipRegion = TryCast(o("ShipRegion"), String)
			Me.ShipVia = CInt(Fix(o("ShipVia")))
			Invoices = CreateInvoices(OrderID, CustomerID)
		End Sub
	End Class
	Public Class CustomersWithDetail
		Inherits Customers
		Private privateOrders As IList(Of OrdersWithDetail)
		Public Property Orders() As IList(Of OrdersWithDetail)
			Get
				Return privateOrders
			End Get
			Private Set(ByVal value As IList(Of OrdersWithDetail))
				privateOrders = value
			End Set
		End Property

		Public Shared Function Exists(ByVal view As DataView, ByVal condition As Predicate(Of DataRowView)) As Boolean
			For Each row As DataRowView In view
				If condition(row) Then
					Return True
				End If
			Next row
			Return False
		End Function
		Public Shared Function CreateCustomersForMaterDetailView(ByVal employeeID As Integer) As IList(Of CustomersWithDetail)
			Dim orders As DataView = NWindData.Orders
			Dim customers As DataView = NWindData.Customers
			Dim dict As Dictionary(Of Integer, Integer) = EmployeesWithPhotoData.OrdersRelationsDictionary
			Dim res As IList(Of CustomersWithDetail) = New List(Of CustomersWithDetail)()
			For Each c As DataRowView In customers
				Dim row As DataRowView = c
				If Exists(orders, Function(order) CStr(order("CustomerID")) = CStr(row("CustomerID")) AndAlso dict(CInt(Fix(order("OrderId")))) = employeeID) Then
					res.Add(New CustomersWithDetail(c, employeeID, True))
				End If
			Next c
			Return res
		End Function
		Public Shared Function CreateCustomers(ByVal employeeID As Integer) As IList(Of CustomersWithDetail)
			Dim orders As DataView = NWindData.Orders
			Dim customers As DataView = NWindData.Customers
			Dim res As IList(Of CustomersWithDetail) = New List(Of CustomersWithDetail)()
			For Each c As DataRowView In customers
				Dim row As DataRowView = c
				If Exists(orders, Function(order) CStr(order("CustomerID")) = CStr(row("CustomerID")) AndAlso CInt(Fix(order("EmployeeID"))) = employeeID) Then
					res.Add(New CustomersWithDetail(c, employeeID))
				End If
			Next c
			Return res
		End Function

		Public Sub New(ByVal c As DataRowView, ByVal employeeID As Integer, Optional ByVal newOrders As Boolean = False)
			Me.Address = TryCast(c("Address"), String)
			Me.City = TryCast(c("City"), String)
			Me.CompanyName = TryCast(c("CompanyName"), String)
			Me.ContactName = TryCast(c("ContactName"), String)
			Me.ContactTitle = TryCast(c("ContactTitle"), String)
			Me.Country = CountryNameResolver.Resolve(TryCast(c("Country"), String))
			Me.CustomerID = TryCast(c("CustomerID"), String)
			Me.Fax = TryCast(c("Fax"), String)
			Me.Phone = TryCast(c("Phone"), String)
			Me.PostalCode = TryCast(c("PostalCode"), String)
			Me.Region = TryCast(c("Region"), String)
			Orders = If(newOrders, OrdersWithDetail.CreateOrdersForMasterDetailView(CustomerID, employeeID), OrdersWithDetail.CreateOrders(CustomerID, employeeID))
		End Sub

	End Class
	Public Class EmployeesWithDetails
		Inherits List(Of EmployeeWithDetails)
		Public Sub New()
			AddRange(EmployeeWithDetails.CreateMasterDetailSource())
		End Sub
	End Class

	Public Class EmployeesWithDetailsForEmbeddedView
		Inherits List(Of EmployeeWithDetails)
		Public Sub New()
			AddRange(EmployeeWithDetails.CreateEmbeddedViewSource())
		End Sub
	End Class
	Public Class EmployeeWithDetails
		Inherits Employees
		Private customersCore As IList(Of CustomersWithDetail)
		Public ReadOnly Property Customers() As IList(Of CustomersWithDetail)
			Get
				If customersCore Is Nothing Then
					customersCore = CustomersWithDetail.CreateCustomers(EmployeeID)
				End If
				Return customersCore
			End Get
		End Property
		Private ordersCore As IList(Of OrdersWithDetail)
		Public ReadOnly Property Orders() As IList(Of OrdersWithDetail)
			Get
				If ordersCore Is Nothing Then
					ordersCore = OrdersWithDetail.CreateOrders("", EmployeeID)
				End If
				Return ordersCore
			End Get
		End Property

		Private mdcustomersCore As IList(Of CustomersWithDetail)
		Public ReadOnly Property MDCustomers() As IList(Of CustomersWithDetail)
			Get
				If mdcustomersCore Is Nothing Then
					mdcustomersCore = CustomersWithDetail.CreateCustomersForMaterDetailView(EmployeeID)
				End If
				Return mdcustomersCore
			End Get
		End Property
		Private mdordersCore As IList(Of OrdersWithDetail)
		Public ReadOnly Property MDOrders() As IList(Of OrdersWithDetail)
			Get
				If mdordersCore Is Nothing Then
					mdordersCore = OrdersWithDetail.CreateOrdersForMasterDetailView("", EmployeeID)
				End If
				Return mdordersCore
			End Get
		End Property

		Private chartSourceCore As IEnumerable(Of ChartPoint)
		Public ReadOnly Property ChartSource() As IEnumerable(Of ChartPoint)
			Get
				If chartSourceCore Is Nothing Then
					chartSourceCore = CreateChartSource()
				End If
				Return chartSourceCore
			End Get
		End Property

		Private Function CreateChartSource() As IEnumerable(Of ChartPoint)
			Dim list As IList(Of ChartPoint) = ( _
					From o In MDOrders _
					Group o By o.OrderDate Into cp = Group _
					Select New ChartPoint() With {.ArgumentMember = OrderDate, .Orders = cp.ToList()}).ToList()
			For Each cp As ChartPoint In list
				Dim value As Decimal = 0
				For Each order As OrdersWithDetail In cp.Orders
					For Each inv As Invoices In order.Invoices
						value += inv.Quantity * inv.UnitPrice
					Next inv
				Next order
				cp.ValueMember = CInt(Fix(value))
			Next cp
			Return list
		End Function
		Private privateEMail As String
		Public Property EMail() As String
			Get
				Return privateEMail
			End Get
			Set(ByVal value As String)
				privateEMail = value
			End Set
		End Property

		Public Shared Function CreateMasterDetailSource() As IList(Of EmployeeWithDetails)
			Dim empls As List(Of Employee) = EmployeesWithPhotoData.DataSource
			Dim res As New List(Of EmployeeWithDetails)()

			For Each employee As Employee In empls
				res.Add(New EmployeeWithDetails(employee))
			Next employee
			InitSubordinateEmployee(res)
			Return res
		End Function
		Private privateSubEmployees As List(Of EmployeeWithDetails)
		Public Property SubEmployees() As List(Of EmployeeWithDetails)
			Get
				Return privateSubEmployees
			End Get
			Set(ByVal value As List(Of EmployeeWithDetails))
				privateSubEmployees = value
			End Set
		End Property
		Shared Sub InitSubordinateEmployee(ByVal res As List(Of EmployeeWithDetails))
			For Each empl As EmployeeWithDetails In res
				Dim emplId As Integer = empl.EmployeeID
				empl.SubEmployees = res.Where(Function(e) e.ParentId = emplId).ToList()
			Next empl
		End Sub
		Public Shared Function CreateEmbeddedViewSource() As IList(Of EmployeeWithDetails)
			Dim employees As DataView = NWindData.Employees
			Dim res As New List(Of EmployeeWithDetails)()
			For Each employee As DataRowView In employees
				res.Add(New EmployeeWithDetails(employee))
			Next employee
			Return res
		End Function

		Public Sub New(ByVal e As DataRowView)
			Address = TryCast(e("Address"), String)
			BirthDate = CDate(e("BirthDate"))
			City = TryCast(e("City"), String)
			Country = TryCast(e("Country"), String)
			EmployeeID = CInt(Fix(e("EmployeeID")))
			Extension = TryCast(e("Extension"), String)
			FirstName = TryCast(e("FirstName"), String)
			HireDate = CDate(e("HireDate"))
			HomePhone = TryCast(e("HomePhone"), String)
			LastName = TryCast(e("LastName"), String)
			Notes = TryCast(e("Notes"), String)
			Photo = CType(e("Photo"), Byte())
			PostalCode = TryCast(CStr(e("PostalCode")), String)
			Region = TryCast(e("Region"), String)
			Title = TryCast(e("Title"), String)
			TitleOfCourtesy = TryCast(e("TitleOfCourtesy"), String)
		End Sub
		Public Sub New(ByVal e As Employee)
			Address = e.AddressLine1
			BirthDate = e.BirthDate
			City = e.City
			Country = CountryNameResolver.Resolve(e.CountryRegionName)
			EmployeeID = e.Id
			FirstName = e.FirstName
			HireDate = e.HireDate
			HomePhone = e.Phone
			LastName = e.LastName
			Photo = e.ImageData
			PostalCode = e.PostalCode
			Region = e.CountryRegionName
			EMail = e.EmailAddress
			Title = e.JobTitle
			Me.ParentId = e.ParentId
		End Sub
		Private privateParentId As Integer
		Friend Property ParentId() As Integer
			Get
				Return privateParentId
			End Get
			Private Set(ByVal value As Integer)
				privateParentId = value
			End Set
		End Property
	End Class

	Public Class ChartPoint
		Private privateArgumentMember As DateTime
		Public Property ArgumentMember() As DateTime
			Get
				Return privateArgumentMember
			End Get
			Friend Set(ByVal value As DateTime)
				privateArgumentMember = value
			End Set
		End Property
		Private privateValueMember As Integer
		Public Property ValueMember() As Integer
			Get
				Return privateValueMember
			End Get
			Set(ByVal value As Integer)
				privateValueMember = value
			End Set
		End Property
		Private privateOrders As IList(Of OrdersWithDetail)
		Friend Property Orders() As IList(Of OrdersWithDetail)
			Get
				Return privateOrders
			End Get
			Set(ByVal value As IList(Of OrdersWithDetail))
				privateOrders = value
			End Set
		End Property
	End Class
	Friend NotInheritable Class CountryNameResolver
		Private Sub New()
		End Sub
		Friend Shared Function Resolve(ByVal countryName As String) As String
			Select Case countryName
				Case "USA"
					Return "United States"
				Case "UK"
					Return "United Kingdom"
				Case Else
					Return countryName
			End Select
		End Function
	End Class

	Public Class EmployeeToOrdersConverter
		Implements IValueConverter
		Private employeeOrders As New Dictionary(Of Employee, IEnumerable(Of Orders))()

		Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
			Dim empl As Employee = TryCast(value, Employee)
			If empl Is Nothing Then
				Return Nothing
			End If
			Dim orders As IEnumerable(Of Orders) = Nothing
			If (Not employeeOrders.TryGetValue(empl, orders)) Then
				orders = OrdersWithDetail.CreateOrdersForMasterDetailView("", empl.Id)
				employeeOrders.Add(empl, orders)
			End If
			Return orders
		End Function

		Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
			Throw New NotImplementedException()
		End Function
	End Class
End Namespace