更好的方法是创建一个 SQL 查询,该查询将基于 Employee ID 字段连接表,并根据日期字段过滤数据。DataGridView 将绑定到将根据该 SQL 查询填充的 DataTable。
示例代码:
Dim FilterStr1 As String = "03/01/2013"
Dim DBPathStr1 As String = "C:\db1.mdb"
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim SqlConnStr1 As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBPathStr1 & ";"
Dim SqlStr1 As String = <SQL>
SELECT
W.[Date]
,W.[Employee ID]
,W.[Activity]
,W.[Hours worked]
,W.[Department]
,W.[Shift]
,W.[Equipment Used]
,E.[Name]
,E.[Middle]
,E.[Last]
FROM [Work] AS W
INNER JOIN [Employees] AS E
ON W.[Employee ID] = E.[Employee ID]
WHERE W.[Date] = #<%= FilterStr1 %>#
</SQL>.Value
Dim OleDBAdap1 As New OleDbDataAdapter(SqlStr1, SqlConnStr1)
Dim SqlDTable1 As New DataTable
OleDBAdap1.Fill(SqlDTable1)
DataGridView1.DataSource = SqlDTable1
OleDBAdap1.Dispose()
End Sub
要使用此代码,您需要使用DBPathStr1
MS Access 数据库的路径更改变量的值,并为变量设置日期值FilterStr1
。
编辑
FilterStr1
可以使用 DateTimePicker 控件分配变量的值。为此,您需要在表单上添加一个 DateTimePicker 控件并添加以下代码。
Private Sub DateTimePicker1_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DateTimePicker1.ValueChanged
FilterStr1 = DateTimePicker1.Value.ToString("dd/MM/yyyy")
End Sub