Created
November 16, 2012 02:59
-
-
Save hidayat365/4083576 to your computer and use it in GitHub Desktop.
The Magic of Data Binding
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
Public Class Form1 | |
''' <summary> | |
''' Refresh Grid Data | |
''' </summary> | |
''' <remarks></remarks> | |
Private Sub RefreshGrid() | |
Try | |
' load database ke dataset | |
' data otomatis tampil di semua control yang bind ke DsSample1 | |
Me.CompaniesTableAdapter1.ClearBeforeFill = True | |
Me.CompaniesTableAdapter1.Fill(Me.DsSample1.Companies) | |
Me.CitiesTableAdapter1.ClearBeforeFill = True | |
Me.CitiesTableAdapter1.Fill(Me.DsSample1.Cities) | |
Catch ex As Exception | |
MessageBox.Show("Kesalahan saat RefreshGrid: " & ex.Message, _ | |
Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error) | |
End Try | |
End Sub | |
''' <summary> | |
''' form load event handler | |
''' </summary> | |
''' <param name="sender"></param> | |
''' <param name="e"></param> | |
''' <remarks></remarks> | |
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load | |
RefreshGrid() | |
End Sub | |
''' <summary> | |
''' Button1_Click untuk menangani add new data | |
''' </summary> | |
''' <param name="sender"></param> | |
''' <param name="e"></param> | |
''' <remarks></remarks> | |
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click | |
Try | |
' tambah row baru ke dataset | |
Me.BindingContext(Me.DsSample1, "Companies").AddNew() | |
' pindahkan cursor ke textbox pertama | |
Me.TextBox1.Focus() | |
Catch ex As Exception | |
' inform user | |
MessageBox.Show("Kesalahan saat Add New Row: " & ex.Message, _ | |
Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error) | |
End Try | |
End Sub | |
''' <summary> | |
''' Button2_Click untuk menyimpan data ke database | |
''' </summary> | |
''' <param name="sender"></param> | |
''' <param name="e"></param> | |
''' <remarks></remarks> | |
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click | |
' validasi data | |
If String.IsNullOrEmpty(TextBox1.Text) _ | |
Or String.IsNullOrEmpty(TextBox2.Text) Then | |
MessageBox.Show("Company Code dan Company Name HARUS terisi", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation) | |
Return | |
End If | |
Try | |
' simpan perubahan di control ke dataset | |
Me.BindingContext(Me.DsSample1, "Companies").EndCurrentEdit() | |
' simpan perubahan di dataset ke database | |
Me.CompaniesTableAdapter1.Update(Me.DsSample1.Companies) | |
' refresh grid | |
RefreshGrid() | |
' inform user | |
MessageBox.Show("Simpan Data BERHASIL", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information) | |
Catch ex As Exception | |
' inform user | |
MessageBox.Show("Kesalahan saat Simpan Data: " & ex.Message, _ | |
Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error) | |
End Try | |
End Sub | |
''' <summary> | |
''' Button3_Click menangani proses hapus data | |
''' </summary> | |
''' <param name="sender"></param> | |
''' <param name="e"></param> | |
''' <remarks></remarks> | |
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click | |
Dim res As DialogResult | |
Dim company, message As String | |
Dim position As Integer | |
Try | |
' tentukan message konfirmasi | |
company = DataGridView1.CurrentRow.Cells("CompanyNameDataGridViewTextBoxColumn").Value | |
message = "Hapus data perusahaan ini [" & company & "] ?" | |
' tanyakan konfirmasi user | |
res = MessageBox.Show(message, Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) | |
' hapus jika user setuju | |
If res = Windows.Forms.DialogResult.Yes Then | |
' hapus data pada posisi cursor di dataset | |
position = Me.BindingContext(Me.DsSample1, "Companies").Position | |
Me.BindingContext(Me.DsSample1, "Companies").RemoveAt(position) | |
' simpan perubahan di dataset ke database | |
Me.CompaniesTableAdapter1.Update(Me.DsSample1.Companies) | |
' commit perubahan di dataset | |
Me.DsSample1.AcceptChanges() | |
' inform user | |
MessageBox.Show("Delete Data BERHASIL", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information) | |
End If | |
Catch ex As Exception | |
MessageBox.Show("Kesalahan saat Hapus Data: " & ex.Message, _ | |
Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error) | |
End Try | |
End Sub | |
''' <summary> | |
''' handle cancel add new row | |
''' </summary> | |
''' <param name="sender"></param> | |
''' <param name="e"></param> | |
''' <remarks></remarks> | |
Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click | |
Try | |
' batalkan perubahan di control ke dataset | |
Me.BindingContext(Me.DsSample1, "Companies").CancelCurrentEdit() | |
' batalkan perubahan di dataset | |
Me.DsSample1.RejectChanges() | |
Catch ex As Exception | |
MessageBox.Show("Kesalahan saat Cancel Edit: " & ex.Message, _ | |
Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error) | |
End Try | |
End Sub | |
End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment