cara membuat master detail dalam datagridview vb.net

 

coding module : -------------------------------------------------------

 

Imports System.Data.SqlClient

 

Module Module1

 

Public Conn As SqlConnection
Public DA As SqlDataAdapter
Public DS As DataSet
Public CMD As SqlCommand
Public DR As SqlDataReader

 

Public Sub Koneksi()
Try
Conn = New SqlConnection("data source=.\sqlexpress;initial catalog=jualbelidb;integrated security=true")
Conn.Open()
Catch ex As Exception
MsgBox(ex.Message)
End
End Try
End Sub
End Module

 

coding form : -----------------------------------------------------------------------------

 

Imports System.Data.SqlClient

 

Public Class LihatData

Private Sub LihatData_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call Koneksi()
CMD = New SqlCommand("select TABLE_NAME from INFORMATION_SCHEMA.TABLES order by 1", Conn)
DR = CMD.ExecuteReader
ListBox2.Items.Clear()
Do While DR.Read
ListBox2.Items.Add(DR.Item(0))
Loop
End Sub

 

Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
DGV.Columns.Clear()
dgv1.Columns.Clear()
Call Koneksi()
DA = New SqlDataAdapter("select * from " & ListBox2.Text & "", Conn)
DS = New DataSet
DA.Fill(DS)
DGV.DataSource = DS.Tables(0)
DGV.ReadOnly = True
End Sub

 

Private Sub DGV_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DGV.CellMouseClick
'On Error Resume Next
Try
Dim namatabel, namakolom, isidata As String
namatabel = ListBox2.Text + "detail"
namakolom = DGV.Columns(0).HeaderText
isidata = DGV.Rows(e.RowIndex).Cells(0).Value

Call Koneksi()
DA = New SqlDataAdapter("select * from " & namatabel & " where " & namakolom & " = '" & isidata & "'", Conn)
DS = New DataSet
DA.Fill(DS)
dgv1.DataSource = DS.Tables(0)
dgv1.ReadOnly = True

Catch ex As Exception
MsgBox("tabel ini tidak memiliki relasi / detail")
End Try
End Sub
End Class