Private Sub listview()
commandSQL = "SELECT * FROM CategoryProduct"
commandSET = New SqlCommand(commandSQL, connectSET)
adapterSET = New SqlDataAdapter(commandSET)
datagetSET = New DataSet
adapterSET.Fill(datagetSET, "Categories")
ListBox1.DataSource = datagetSET.Tables("Categories")
ListBox1.DisplayMember = "categoryName"
ListBox1.SelectedIndex = 0
End Sub
ต่อมาโค๊ด Datagrid ครับ
Code
Private Sub dataview(ByVal catID As Integer)
If Not (datagetSET.Tables("Product") Is Nothing) Then
datagetSET.Tables("Product").Clear()
End If
commandSQL = "SELECT p.productID, cp.categoryName, p.productName FROM Product p join CategoryProduct cp on p.categoryPID = cp.categoryPID WHERE p.categoryPID = " & (catID + 1)
commandSET.CommandText = commandSQL
adapterSET.SelectCommand = commandSET
adapterSET.Fill(datagetSET, "Product")
DataGridView1.DataSource = datagetSET.Tables("Product")
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
'bindingSET.Position = ListBox1.SelectedIndex
dataview(ListBox1.SelectedIndex)
End Sub
Private Sub dataview(ByVal catID As String)
If Not (datagetSET.Tables("Product") Is Nothing) Then
datagetSET.Tables("Product").Clear()
End If
commandSQL = "SELECT p.productID, cp.categoryName, p.productName FROM Product p join CategoryProduct cp on p.categoryPID = cp.categoryPID WHERE p.categoryPID = " & catID
commandSET.CommandText = commandSQL
adapterSET.SelectCommand = commandSET
adapterSET.Fill(datagetSET, "Product")
DataGridView1.DataSource = datagetSET.Tables("Product")
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
dataview(ListBox1.GetItemText(ListBox1.SelectedItem))
End Sub