Public Class BindingSet
Public BindingArray As New Dictionary(Of String, BindingSource)
Sub Qry(BindingName As String, SQLString As String)
DA = New SqlDataAdapter(SQLString, CONN)
Dim commandBuild As New SqlCommandBuilder(DA)
Dim retTable As New DataTable
DA.Fill(retTable)
If BindingArray.ContainsKey(BindingName) Then
BindingArray(BindingName).DataSource = retTable
Else
Dim retBinding As New BindingSource
retBinding.DataSource = retTable
BindingArray.Add(BindingName, retBinding)
End If
End Sub
End Class
'---------------------- ฟอร์ม1 Login เพิ่มลบข้อมูล
Public Class _Login
Dim bd As New BindingSet
Private Sub _Login_Load(sender As Object, e As EventArgs) Handles MyBase.Load
bd.Qry("bdLogin", "select * from tbLogin")
GridControl1.DataSource = bd.BindingArray("bdLogin")
End Sub
Private Sub ADD_Click(sender As Object, e As EventArgs) Handles SimpleButton1.Click
SQL = "insert into tbLogin (UserID,UserName,UserPwd,Permis) values
('" & UserID & "','" & txtUser.Text & "','" & txtPass.Text & "','5')"
bd.Qry("bdLogin", SQL)
bd.Qry("bdLogin", "SELECT * FROM tbLogin")
End Sub
Private Sub DEL_Click(sender As Object, e As EventArgs) Handles SimpleButton2.Click
SQL = "delete from tbLogin where UserName='" & txtUser.Text & "'"
bd.Qry("bdLogin", SQL)
bd.Qry("bdLogin", "SELECT * FROM tbLogin")
End Sub
End Class
'-------------------- ฟอร์ม2 ดูข้อมูล Login
Public Class _LoginView
Private Sub _LoginView_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim bd As New BindingSet
bd.Qry("bdLogin", "select * from tbLogin")
GridControl1.DataSource = bd.BindingArray("bdLogin")
End Sub
End Class
ตอนนี้คิดว่าปัญหาติดอยู่ตรง Dim New BindingSet แต่ยังนึกไม่ออกว่าจะแก้ยังไง
หากฟอร์ม 2 เปิดก่อนก็ต้องการให้สร้าง BindingSource ก่อน แล้วอีกฟอร์มดึงไปใช้ร่วมกัน