Public Class Form4
Dim cnn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\PostCode.mdb;Persist Security Info=True;Jet OLEDB:Database Password=4410210091"
Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.DataSource = GetTableOLEDB("SELECT * FROM Sett_Post_Province", cnn)
DataGridView2.DataSource = GetTableOLEDB("SELECT * FROM Sett_Post_PostCode", cnn)
End Sub
Private Function GetTableOLEDB(SQL As String, strConn As String) As Data.DataTable
Dim conn As New Data.OleDb.OleDbConnection(strConn)
Dim ds As New System.Data.DataSet()
conn.Open()
Dim strQuery As String = SQL
Dim adapter As New Data.OleDb.OleDbDataAdapter(strQuery, conn)
adapter.Fill(ds)
Return ds.Tables(0)
End Function
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
DataGridView2.DataSource = GetTableOLEDB("SELECT * FROM Sett_Post_PostCode where Pnv =" & DataGridView1(0, e.RowIndex).Value, cnn)
End Sub
End Class
Private Function GetTableOLEDB(SQL As String, strConn As String) As Data.DataTable
Dim conn As New Data.OleDb.OleDbConnection(strConn)
Dim ds As New System.Data.DataSet()
conn.Open()
Dim strQuery As String = SQL
Dim adapter As New Data.OleDb.OleDbDataAdapter(strQuery, conn)
adapter.Fill(ds)
Return ds.Tables(0)
End Function
ไม่รู้ว่าศัพย์มันคืออะไรแต่ผมชอบเรียกมันว่า Function
อย่าง
Private Function GetTableOLEDB(SQL As String, strConn As String) As Data.DataTable