Created
November 21, 2012 07:16
-
-
Save hidayat365/4123546 to your computer and use it in GitHub Desktop.
The Magic of Data Binding - Part 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Imports System.Data | |
Imports System.Data.SqlServerCe | |
''' <summary> | |
''' Interface IDataAccess | |
''' </summary> | |
''' <remarks>Untuk keseragaman metode akses data</remarks> | |
Public Interface IDataAccess | |
Function GetData() As DataTable | |
Function Fill(ByRef dt As DataTable) As Integer | |
Function Update(ByRef dt As DataTable) As Integer | |
End Interface | |
''' <summary> | |
''' Data Access Layer ke table Contacts | |
''' </summary> | |
''' <remarks>Data Access Layer ke table Contacts, implements IDataAccess</remarks> | |
Public Class CContacts | |
Implements IDataAccess | |
Private QueryString = "SELECT * FROM Contacts" | |
Function Fill(ByRef dt As DataTable) As Integer Implements IDataAccess.Fill | |
Try | |
dt.Clear() | |
dt.Merge(Me.GetData()) | |
Catch ex As Exception | |
Throw New ApplicationException("Exception Occured: " & ex.Message) | |
End Try | |
Return dt.Rows.Count | |
End Function | |
Function GetData() As DataTable Implements IDataAccess.GetData | |
Dim dt As New DataTable | |
Try | |
Dim cn As New SqlCeConnection(My.MySettings.Default.dbAksesConnectionString) | |
Dim da As New SqlCeDataAdapter(QueryString, cn) | |
dt.TableName = "Contacts" | |
dt.Clear() | |
da.Fill(dt) | |
Catch ex As Exception | |
Throw New ApplicationException("Exception Occured: " & ex.Message) | |
End Try | |
Return dt | |
End Function | |
Function Update(ByRef dt As DataTable) As Integer Implements IDataAccess.Update | |
Try | |
Dim cn As New SqlCeConnection(My.MySettings.Default.dbAksesConnectionString) | |
Dim da As New SqlCeDataAdapter(QueryString, cn) | |
Dim cb As New SqlCeCommandBuilder(da) | |
da.Update(dt) | |
Catch ex As Exception | |
Throw New ApplicationException("Exception Occured: " & ex.Message) | |
End Try | |
Return dt.Rows.Count | |
End Function | |
End Class | |
''' <summary> | |
''' Form Utama | |
''' </summary> | |
''' <remarks>Form Utama</remarks> | |
Public Class Form1 | |
Dim kontak As New CContacts | |
Dim data As New DataTable | |
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load | |
' isi data | |
kontak.Fill(data) | |
' binding ke grid | |
DataGridView1.DataSource = data | |
End Sub | |
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click | |
' pastikan perubahan data yang dilakukan user melalui | |
' user interface sudah dipindahkan ke dataset | |
Me.BindingContext(data).EndCurrentEdit() | |
' simpan perubahan data di dataset ke database | |
Me.kontak.Update(data) | |
' update sukses, terima perubahan di dataset | |
data.AcceptChanges() | |
' ambil ulang data dari database (refresh kolom ID) | |
kontak.Fill(data) | |
End Sub | |
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click | |
' pastikan perubahan data yang dilakukan user melalui | |
' user interface *tidak* dipindahkan ke dataset | |
Me.BindingContext(data).CancelCurrentEdit() | |
' batalkan semua perubahan di dataset | |
data.RejectChanges() | |
End Sub | |
End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment