Pada modul sebelumnya kita telah belajar bagaimana menggunakan DataViewManager untuk menampilkan data dalam DataGrid yang memiliki Relasi antara DataTable Master dan Child.
Pada Modul ini kita masih akan bekerja dengan DataViewManager untuk keperluaan pembuatan Form Entry data dalam bentuk Master dan Child.
Contoh hasil Master dan Detail yang ditampilkan pada DataGrid1.
Pada Modul ini kita masih akan bekerja dengan DataViewManager untuk keperluaan pembuatan Form Entry data dalam bentuk Master dan Child.
Contoh hasil Master dan Detail yang ditampilkan pada DataGrid1.
Mempersiapkan data source
Dalam hal ini kita akan membuka koneksi dan mempersiapkan DataSet, membuat Relation antara master DataTable dan child, serta membuat DataViewManager. Akhirnya di binding ke DataGrid.Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Oledbconn = New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=D:\BelajarVBnet\Belajar.mdb;") ODABarang = New OleDbDataAdapter("Select * from Barang", Oledbconn) ODAPelanggan = New OleDbDataAdapter("Select * from Pelanggan", Oledbconn) ODABarang.Fill(ODS, "Barang") ODAPelanggan.Fill(ODS, "Pelanggan") 'Buat Relation antara DataTable Barang (Master) dan Pelanggan (Child) ODS.Relations.Add("PelangganBarang", ODS.Tables("Barang").Columns("KodeBar"), ODS.Tables("Pelanggan").Columns("KodeBar")) 'Membuat DataViewManager untuk DataSet ODVM = New DataViewManager(ODS) 'Binding DataViewManager ke DataGrid, DataTable Barang Me.TextBox1.DataBindings.Add(New System.Windows.Forms.Binding("text", ODVM, "Barang.KodeBar")) Me.TextBox2.DataBindings.Add(New System.Windows.Forms.Binding("text", ODVM, "Barang.Keterangan")) Me.TextBox3.DataBindings.Add(New System.Windows.Forms.Binding("text", ODVM, "Barang.Satuan")) Me.TextBox4.DataBindings.Add(New System.Windows.Forms.Binding("text", ODVM, "Barang.Harga")) DataGrid1.SetDataBinding(ODVM, "Barang.PelangganBarang") End Sub
MoveFirst, Previous, Next dan Last
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click Me.BindingContext(ODVM, "Barang").Position = 0 End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click Me.BindingContext(ODVM, "Barang").Position = ODVM.DataSet.Tables("Barang").Rows.Count End Sub
Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click Me.BindingContext(ODVM, "Barang").Position -= 1 End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click Me.BindingContext(ODVM, "Barang").Position += 1 End Sub
Add, Update, Delete dan Cancel
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click Me.BindingContext(ODVM, "Barang").AddNew() End Sub
Private Sub btnUpd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpd.Click Me.BindingContext(ODVM, "Barang").EndCurrentEdit() End Sub
Private Sub btnCnc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCnc.Click Me.BindingContext(ODVM, "Barang").CancelCurrentEdit() End Sub
Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click If (Me.BindingContext(ODVM, "Barang").Count > 0) Then Me.BindingContext(ODVM, "Barang").RemoveAt(Me.BindingContext(ODVM, "Barang").Position) End If End Sub
Update data ke data source
Karena DataSet terdiri dari dua tabel, maka kita perlu mengupdatenya satu-persatu melalui DataAdapter masing-masing. Private Sub Form4_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
Dim oleCB As New OleDbCommandBuilder(ODABarang)
Dim oleCP As New OleDbCommandBuilder(ODAPelanggan)
'Update ke masing-masing data source
ODABarang.Update(ODS, "Barang")
ODAPelanggan.Update(ODS, "Pelanggan")
End Sub
Program Kongkrit
Imports System.Data.OleDb Public Class Form4 Inherits System.Windows.Forms.Form Dim Oledbconn As OleDbConnection Dim ODABarang As OleDbDataAdapter Dim ODAPelanggan As OleDbDataAdapter Dim ODS As New DataSet Dim ODVM As New DataViewManager
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Oledbconn = New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=D:\BelajarVBnet\Belajar.mdb;") ODABarang = New OleDbDataAdapter("Select * from Barang", Oledbconn) ODAPelanggan = New OleDbDataAdapter("Select * from Pelanggan", Oledbconn) ODABarang.Fill(ODS, "Barang") ODAPelanggan.Fill(ODS, "Pelanggan") 'Buat Relation antara DataTable Barang (Master) dan Pelanggan (Child) ODS.Relations.Add("PelangganBarang", ODS.Tables("Barang").Columns("KodeBar"), ODS.Tables("Pelanggan").Columns("KodeBar")) 'Membuat DataViewManager untuk DataSet ODVM = New DataViewManager(ODS) 'Binding DataViewManager ke DataGrid, DataTable Barang Me.TextBox1.DataBindings.Add(New System.Windows.Forms.Binding("text", ODVM, "Barang.KodeBar")) Me.TextBox2.DataBindings.Add(New System.Windows.Forms.Binding("text", ODVM, "Barang.Keterangan")) Me.TextBox3.DataBindings.Add(New System.Windows.Forms.Binding("text", ODVM, "Barang.Satuan")) Me.TextBox4.DataBindings.Add(New System.Windows.Forms.Binding("text", ODVM, "Barang.Harga")) DataGrid1.SetDataBinding(ODVM, "Barang.PelangganBarang") End Sub
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click Me.BindingContext(ODVM, "Barang").Position = 0 End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click Me.BindingContext(ODVM, "Barang").Position = ODVM.DataSet.Tables("Barang").Rows.Count End Sub
Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click Me.BindingContext(ODVM, "Barang").Position -= 1 End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click Me.BindingContext(ODVM, "Barang").Position += 1 End Sub
Private Sub Form4_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed Dim oleCB As New OleDbCommandBuilder(ODABarang) Dim oleCP As New OleDbCommandBuilder(ODAPelanggan) ODABarang.Update(ODS, "Barang") ODAPelanggan.Update(ODS, "Pelanggan") End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click Me.BindingContext(ODVM, "Barang").AddNew() End Sub
Private Sub btnUpd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpd.Click Me.BindingContext(ODVM, "Barang").EndCurrentEdit() End Sub
Private Sub btnCnc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCnc.Click Me.BindingContext(ODVM, "Barang").CancelCurrentEdit() End Sub
Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click If (Me.BindingContext(ODVM, "Barang").Count > 0) Then Me.BindingContext(ODVM, "Barang").RemoveAt(Me.BindingContext(ODVM, "Barang").Position) End If End Sub End Class
Tidak ada komentar :
Posting Komentar