Minggu, 21 Juli 2013

Program Data Form Entry

Pada modul ini kita akan membuat Entry data dengan menggunakan DataView, yang dibinding pada masing-masing textbox.

Mempersiapkan data source

Dalam hal ini kita akan membuka koneksi dan mempersiapkan DataView, serta binding masing-masing textbox ke data field.
    Private Sub Form2_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;")
        sSQL = "Select * From Barang"
        OleDbComm = New OleDbCommand(sSQL, OleDbConn)
        OleDA = New OleDbDataAdapter(OleDbComm)
        OleDA.Fill(ODS, "Barang")
        ODV = New DataView(ODS.Tables("Barang"))
        'Melakukan binding masing-masing textbox 
        Me.TextBox1.DataBindings.Add(New System.Windows.Forms.Binding("Text", ODV, "KodeBar"))
        Me.TextBox2.DataBindings.Add(New System.Windows.Forms.Binding("Text", ODV, "Keterangan"))
        Me.TextBox3.DataBindings.Add(New System.Windows.Forms.Binding("Text", ODV, "Satuan"))
        Me.TextBox4.DataBindings.Add(New System.Windows.Forms.Binding("Text", ODV, "Harga"))
    End Sub

MoveFirst

    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        Me.BindingContext(ODV).Position = 0
    End Sub

MovePrev

    Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
        Me.BindingContext(ODV).Position -= 1
    End Sub

MoveNext

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        Me.BindingContext(ODV).Position += 1
    End Sub

MoveLast

    Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
        Me.BindingContext(ODV).Position = ODV.Table.Rows.Count
    End Sub

AddNew

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.BindingContext(ODV).AddNew()
        Me.Update()
    End Sub

Update

    Private Sub btnUpd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.BindingContext(ODV).EndCurrentEdit()
    End Sub

Cancel

    Private Sub btnCnc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.BindingContext(ODV).CancelCurrentEdit()
    End Sub

Delete

    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If (Me.BindingContext(ODV).Count > 0) Then
            Me.BindingContext(ODV).RemoveAt(Me.BindingContext(ODV).Position)
        End If
    End Sub

Update data ke data source

Sesuatu hal yang harus anda pahami adalah bahwa perubahan pada DataView tidak secara langsung terupdate pada data source..
    Private Sub Form2_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        Dim oleCommBuilder As New OleDbCommandBuilder(OleDA)
        OleDA.Update(ODS, "Barang")
    End Sub

Program Kongkrit


Imports System.Data.OleDb
Public Class Form2
    Inherits System.Windows.Forms.Form
    Dim OleDbConn As OleDbConnection
    Dim OleDbComm As OleDbCommand
    Dim OleDA As OleDbDataAdapter
    Dim ODS As New DataSet
    Dim ODV As DataView
    Dim sSQL As String
    Private Sub Form2_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;")
        sSQL = "Select * From Barang"
        OleDbComm = New OleDbCommand(sSQL, OleDbConn)
        OleDA = New OleDbDataAdapter(OleDbComm)
        OleDA.Fill(ODS, "Barang")
        ODV = New DataView(ODS.Tables("Barang"))
        Me.TextBox1.DataBindings.Add(New System.Windows.Forms.Binding("Text", ODV, "KodeBar"))
        Me.TextBox2.DataBindings.Add(New System.Windows.Forms.Binding("Text", ODV, "Keterangan"))
        Me.TextBox3.DataBindings.Add(New System.Windows.Forms.Binding("Text", ODV, "Satuan"))
        Me.TextBox4.DataBindings.Add(New System.Windows.Forms.Binding("Text", ODV, "Harga"))
    End Sub
    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        Me.BindingContext(ODV).Position = 0
    End Sub
    Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
        Me.BindingContext(ODV).Position = ODV.Table.Rows.Count
    End Sub
    Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
        Me.BindingContext(ODV).Position -= 1
    End Sub
    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        Me.BindingContext(ODV).Position += 1
    End Sub
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.BindingContext(ODV).AddNew()
        Me.Update()
    End Sub
    Private Sub btnUpd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.BindingContext(ODV).EndCurrentEdit()
    End Sub
    Private Sub Form2_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        Dim oleCommBuilder As New OleDbCommandBuilder(OleDA)
        OleDA.Update(ODS, "Barang")
    End Sub
    Private Sub btnCnc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.BindingContext(ODV).CancelCurrentEdit()
    End Sub
    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If (Me.BindingContext(ODV).Count > 0) Then
            Me.BindingContext(ODV).RemoveAt(Me.BindingContext(ODV).Position)
        End If
    End Sub
End Class

Tidak ada komentar :

Posting Komentar