Predicates
I recently read an article in the MSDN magazine about Predicates and Actions written by Ken Getz. I was thinking that predicates will make it easy to create a master details datagridview for a business object. This example creates 2 classes Customers and Orders. I used the Northwind database to fill a list of customers and a list of orders. I bound one grid to a binding source who's datasource is the list of customers. In the binding sources position changed event I use the list of orders findall method to show all the orders for a customer.
The classes
Public Class Orders
Private mintOrderID As Integer
Public Property OrderID() As Integer
Get
Return mintOrderID
End Get
Set(ByVal value As Integer)
mintOrderID = value
End Set
End Property
Private mdtOrderDate As Date
Public Property OrderDate() As Date
Get
Return mdtOrderDate
End Get
Set(ByVal value As Date)
mdtOrderDate = value
End Set
End Property
Private mstrCustID As String
Public Property CustomerID() As String
Get
Return mstrCustID
End Get
Set(ByVal value As String)
mstrCustID = value
End Set
End Property
End Class
Public Class Customers
Private mstrCustID As String
Public Property CustomerID() As String
Get
Return mstrCustID
End Get
Set(ByVal value As String)
mstrCustID = value
End Set
End Property
Private mstrCompanyName As String
Public Property CompanyName() As String
Get
Return mstrCompanyName
End Get
Set(ByVal value As String)
mstrCompanyName = value
End Set
End Property
Private mstrContactName As String
Public Property ContactName() As String
Get
Return mstrContactName
End Get
Set(ByVal value As String)
mstrContactName = value
End Set
End Property
End Class
The Main code
Imports System.Data.SqlClient
Public Class Form1
Dim lstCustomers As New List(Of Customers)
Dim lstOrders As New List(Of Orders)
Dim WithEvents bsCustomers As New BindingSource
Private CustId As String = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FillCustomers()
FillOrders()
bsCustomers.DataSource = lstCustomers
DataGridView1.DataSource = bsCustomers
End Sub
Private Sub FillCustomers()
Dim conn As SqlConnection
Dim strConn As String
Dim dr As SqlDataReader
Dim cmd As SqlCommand
Dim strSql As String
strConn = "Server = .;Database = NorthWind; Integrated Security = SSPI;"
conn = New SqlConnection(strConn)
conn.Open()
strSql = "Select CustomerID, CompanyName, ContactName from Customers"
cmd = New SqlCommand(strSql, conn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
Do While dr.Read
Dim cls As New Customers
With cls
.CompanyName = dr("CompanyName").ToString
.ContactName = dr("ContactName").ToString
.CustomerID = dr("CustomerID").ToString
End With
lstCustomers.Add(cls)
Loop
conn.Close()
End Sub
Private Sub FillOrders()
Dim conn As SqlConnection
Dim strConn As String
Dim dr As SqlDataReader
Dim cmd As SqlCommand
Dim strSql As String
strConn = "Server = .;Database = NorthWind; Integrated Security = SSPI;"
conn = New SqlConnection(strConn)
conn.Open()
strSql = "Select CustomerID, OrderID, OrderDate from Orders"
cmd = New SqlCommand(strSql, conn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
Do While dr.Read
Dim cls As New Orders
With cls
.OrderID = dr.GetInt32(1)
.OrderDate = dr.GetDateTime(2)
.CustomerID = dr("CustomerID").ToString
End With
lstOrders.Add(cls)
Loop
conn.Close()
End Sub
Private Function FindCustomerOrders(ByVal Ordr As Orders) As Boolean
Return Ordr.CustomerID = CustId
End Function
Private Sub bsCustomers_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles bsCustomers.PositionChanged
Dim cust As Customers
cust = TryCast(bsCustomers.Current, Customers)
If Not cust Is Nothing Then
CustId = cust.CustomerID
DataGridView2.DataSource = lstOrders.FindAll(AddressOf FindCustomerOrders)
End If
End Sub
End Class