Lanjur dari Part 3, bakal selesai sudah artikel tentang CRUD VB.NET dengan database Microsoft Access ini, semoga bermanfaat yah readers. Ini dia code lengkap nya....
Imports System.Data.OleDb
Public Class frmCategory
Dim con As New OleDbConnection
Sub Open_Koneksi()
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0" & _
";Data Source=latihan.accdb;" & _
"Persist Security Info=False;"
con.Open()
End Sub
Sub Data_Load()
Dim cmd As New OleDbCommand
Dim adapt As New OleDbDataAdapter
Dim dt As New DataTable
Try
If Not con.State = ConnectionState.Open Then Open_Koneksi()
cmd.Connection = con
cmd.CommandText = "SELECT kodekategori, namakategori FROM kategori"
adapt.SelectCommand = cmd
adapt.Fill(dt)
With grdData
.AllowUserToAddRows = False
.ReadOnly = True
.DataSource = dt
End With
Catch ex As OleDbException
MessageBox.Show("Error: " & ex.Message)
Finally
con.Close()
End Try
End Sub
Private Sub frmCategory_Load(sender As Object, _
e As EventArgs) Handles Me.Load
Data_Load()
End Sub
Private Sub tbrSave_Click(sender As Object, _
e As EventArgs) Handles tbrSave.Click
Dim myCommand As New OleDbCommand
Dim SQL As String
Try
If Not con.State = ConnectionState.Open Then Open_Koneksi()
If tbrEdit.Enabled = True Then
SQL = "INSERT INTO kategori (kodekategori,namakategori) VALUES" & _
" ('" & txtCode.Text & "', '" & txtName.Text & "')"
Else
SQL = "UPDATE kategori SET namakategori='" & txtName.Text & "'" & _
" WHERE kodekategori = '" & txtCode.Text & "'"
End If
myCommand.Connection = con
myCommand.CommandText = Sql
myCommand.ExecuteNonQuery()
If tbrEdit.Enabled = True Then
MsgBox("Data baru tersimpan")
Else
MsgBox("Perubahan tersimpan")
End If
tbrCancel_Click(Nothing, Nothing)
con.Close()
Catch myerror As OleDbException
MessageBox.Show("Error: " & myerror.Message)
Finally
con.Dispose()
End Try
End Sub
Private Sub grdData_CellMouseDoubleClick(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) _
Handles grdData.CellMouseDoubleClick
txtCode.Text = grdData.CurrentRow.Cells(0).Value
txtName.Text = grdData.CurrentRow.Cells(1).Value
tbrEdit.Enabled = False
txtCode.ReadOnly = True
End Sub
Private Sub tbrEdit_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles tbrEdit.Click
grdData_CellMouseDoubleClick(Nothing, Nothing)
End Sub
Private Sub tbrCancel_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles tbrCancel.Click
txtCode.Text = String.Empty
txtName.Text = String.Empty
tbrEdit.Enabled = True
txtCode.ReadOnly = False
Data_Load()
End Sub
Private Sub tbrDelete_Click(sender As Object, _
e As EventArgs) Handles tbrDelete.Click
If MsgBox("Yakin akan menghapus data?", MsgBoxStyle.YesNo, _
"Konfirmasi") = MsgBoxResult.No Then Exit Sub
Dim myCommand As New OleDbCommand
Dim SQL As String
Try
If Not con.State = ConnectionState.Open Then Open_Koneksi()
SQL = "DELETE FROM kategori WHERE kodekategori = " & _
"'" & grdData.CurrentRow.Cells(0).Value & "'"
myCommand.Connection = con
myCommand.CommandText = SQL
myCommand.ExecuteNonQuery()
MsgBox("Data terhapus")
tbrCancel_Click(Nothing, Nothing)
con.Close()
Catch myerror As OleDbException
MessageBox.Show("Error: " & myerror.Message)
Finally
con.Dispose()
End Try
End Sub
Private Sub tbrClose_Click(sender As Object, _
e As EventArgs) Handles tbrClose.Click
Me.Close()
End Sub
End Class
Happy Coding ^_^
Click here if you like this article.
12 Comments
kayak nya mesti INSERT bukan inser ^_^
Gimana ya cara penambahan stock dari gridview secara bersamaan/ lebih dari satu row tanpa memanggil data nya ke gridview.
Logikanya gini mbak, saya punya satu form isinya datagrid(barcode,nama,qty) ama button tambah stock, jadi pas data di input saya tampung di gridview setelah selesai penginputan data di tambah stock ke database dengan cara " cari data dari masterstock(tabel) where/berdasar kolom dtagrid kolom(0) dan apabila dataditemukan di maka data tersebut tambahkan/kurangkan dengan kolom(2)/kolom qty.
Terima kasih mbak mohon pencerahan nya...
Contoh: UPDATE tblstock SET qty = qty + 1 WHERE barcode = 'xxx'
ganti angka 1 dengan nilai qty yang ada di grid, dan xxx dengan barcode yang ada di grid.
Select * from master_stock where barcode='" & dvg.currentrows.cell(0).value &.....
If Dr.hasrows then
kolom(2) /kolom Qty tambahkan dengan Dr.items("stock")
Tapi gak jalan mbak.
Klo query select sih buat menampilkan data aja ga mengubah data.
Klo perubahan nya pengen ada efek ke database, perubahan data qty dengan query update atau klo pengen penambahan row ya pake insert (tergantung gimana bentuk tabel di database nya)
Klo pengen perubahan temporary aja, coba datasource di grid nya pake datatable aja.
Gimana klo jelasin lebih detail mulai dari bentuk table nya, UI, dan code yang udah dibuat mungkin?
table_Stock
barcode
001
002
001
nama
kopi
gula
kecap
stock
20
30
40
datagrid:
barcode
001
002
nama
kopi
gula
qty
10
10
nah begitu data diupdate tambah stock maka semua qty menjumlahkan ke tableStock
contoh
qty= 10 + 10 + Stock(barcode,001) 20 = 40
qty= 10 + 10 + Stock(barcode,002) 30 = 50
seharusnya kayak gini
qty= 10 + Stock(barcode,001) 20 = 30
qty= 10 + Stock(barcode,002) 30 = 40
ini koding nya mbak apa ada yang salah ya.
Sub TambahStock()
Call Koneksi()
For Baris As Integer = 0 To DGV.Rows.Count - 2
Dim ubah As String = "UPDATE Master_Stock SET Stock=Stock + '" & DGV.Rows(Baris).Cells(4).Value & "' WHERE Barcode='" & DGV.Rows(Baris).Cells(0).Value & "'"
CMD = New SqlCommand(ubah, CONN)
CMD.ExecuteNonQuery()
Next
End Sub
Terima kasih ya bu..
Oya bu kalo ada toturial marge cell/ marge rows datagrid vb net ditunggu postingan nya...