DATAGRIDVIEW. Menampilkan Data dari database Ke Datagridview

12  52  Download (0)

Teks penuh

(1)

DATAGRIDVIEW

DataGridView adalah sebuah kontrol di VB .net yang digunakan untuk menampilkan data dalam bentuk grid. Control ini dapat dikoneksikan dengan berbagai jenis database, dan menampilkan isi dari tabel atau query melalui perintah SQL.

Menampilkan Data dari database Ke Datagridview

Langkah-langkahnya sebagai berikut :

 Pilih Datagridview pada toolbox

(2)

 Pilih New Connection

 Pilih server name sesuai dengan nama servernya. Pilih nama database yang akan dikoneksikan. Kemudian klik Test Connection untuk memastikan koneksi sudah berhasil.

(3)
(4)

Membuat Data Grid View Sebagai Lembar Kerja

Menghitung Total pada GridView dengan LINQ

LINQ ( Language Integrated Query ) adalah sebuah Query yang terintegrasi dengan IDE Visual Studio, sehingga para programmer mempunyai opsi lain untuk melakukan sebuah query tanpa harus membuat command sql dalam bentuk string.

Banyak jenis LINQ yang dapat dipakai, LINQ to SQL, LINQ to XML, LINQ to Dataset dan lain – lain. Salah satu kegunaan LINQ yang sering sekali dipakai adalah untuk melakukan penjumlahan pada GridView, biasanya ini sering digunakan pada aplikasi seperti POS dan aplikasi sejenis.

Fungsi untuk menjumlahkan grid dengan looping pada tombol total : Dim Total AsDouble = 0

ForEach row As DataGridViewRow In DataGridView1.Rows 'Index no 1 kolom jumlah

Total += Convert.ToDouble(row.Cells(1).Value) Next

txtTotal.Text = Total

Atau, bandingkan apabila kita memakai LINQ….

Dim Total = Aggregate row As DataGridViewRow In DataGridView1.Rows _ Into Sum(Convert.ToDouble(row.Cells(1).Value))

txtTotal.Text = Total

(5)

Memasukan Data Dari DataGridView Ke Database

Buat database terlebih dahulu, misalnya kita ambil database DataMhs dengan tabel mhs. Kemudian buat form dibawah ini.

Data sebelum diubah :

Data sesudah diubah :

Masukan coding ke form dibawah ini :

Imports System.Data.SqlClient Imports System.Data

Public Class Form1

Dim sCommand As SqlCommand Dim sAdapter As SqlDataAdapter Dim sBuilder As SqlCommandBuilder Dim sDs As DataSet

(6)

Dim sTable As DataTable

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim connectionString As String = "Data Source=(LOCAL); Initial Catalog=DataMhs; Integrated Security=True"

Using connection As New SqlConnection(connectionString)

Dim cmdText As String = "INSERT INTO mhs (nim, nama, jenis_kelamin) VALUES (@Column1, @Column2, @Column3)"

Dim command As New SqlCommand(cmdText, connection)

command.Parameters.Add(New SqlParameter("@Column1", SqlDbType.VarChar)) command.Parameters.Add(New SqlParameter("@Column2", SqlDbType.VarChar)) command.Parameters.Add(New SqlParameter("@Column3", SqlDbType.VarChar)) connection.Open()

Dim transaction As SqlTransaction = connection.BeginTransaction() command.Transaction = transaction

Try

For i As Integer = 0 To DataGridView1.Rows.Count - 2 command.Parameters("@Column1").Value = DataGridView1.Rows(i).Cells(0).FormattedValue command.Parameters("@Column2").Value = DataGridView1.Rows(i).Cells(1).FormattedValue command.Parameters("@Column3").Value = DataGridView1.Rows(i).Cells(2).FormattedValue command.ExecuteNonQuery() Next i transaction.Commit()

MessageBox.Show("Data Berhasil Disimpan") Catch ex As Exception

Try

transaction.Rollback()

Catch rollBackEx As Exception

MessageBox.Show(rollBackEx.Message) End Try End Try End Using End Sub End Class

(7)

Pencarian Kata Dengan Textbox Di Datagridview

Pencarian dimana user mengetikan kata dalam sebuah textbox, sementara kursor didatagrid akan mencari kata yang sama sesuai dengan kata dalam textbox.

proses pencarian gambar diatas adalah pencarian Nomor Akun. ketika kita mengetikan nim yang kita inginkan, kursor yang berwarna biru di dalam grid akan menunjukkannya. Listing Programnya adalah sebagai berikut :

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me.MhsTableAdapter.Fill(Me.DataMhsDataSet.mhs) End Sub

Private Sub txtFind_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFind.TextChanged

Dim xloop1 As Integer Dim sPola As String Dim sbuka As String Dim SBetul As Boolean sPola = txtFind.Text + "*"

For xloop1 = 1 To Gridme.RowCount - 1

sbuka = Gridme.Rows(xloop1 - 1).Cells(0).Value SBetul = UCase(sbuka) Like UCase(sPola)

If SBetul = True Then

Gridme.CurrentCell = Gridme.Item(0, xloop1 - 1) Exit Sub

End If Next End Sub End Class

(8)

LIST VIEW

ListView dalam Visual Basic biasanya digunakan untuk menampilkan data, baik dari database maupun bukan, ke dalam tabel dalam bentuk list atau grid. Dengan listview, data yang ada dapat diurutkan, ditambahkan maupun dihapus dengan mudah dari list.

Menambah, Mengedit dan Menghapus Isi List View

Jawaban :

Public Class FrmListView Dim Baris As Integer = 0 Dim BarisEdit As Integer = 0

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Mengatur ListView

ListView1.View = View.Details ListView1.FullRowSelect = True ListView1.GridLines = True

ListView1.Columns.Add("Nama Lengkap", 150) ListView1.Columns.Add("Alamat", 200) End Sub

Button Tambah

Private Sub BtnTambah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnTambah.Click

ListView1.Items.Add(TextBox1.Text)

ListView1.Items(Baris).SubItems.Add(TextBox2.Text) Baris += 1

(9)

Button Edit

Private Sub BtnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEdit.Click ListView1.Items(BarisEdit).Text = TextBox1.Text ListView1.Items(BarisEdit).SubItems(1).Text = TextBox2.Text End Sub

ListView

Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

BarisEdit = ListView1.FocusedItem.Index End Sub

Button Hapus

Private Sub BtnHapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnHapus.Click

ListView1.Items(BarisEdit).Remove() End Sub

End Class

Tugas Prktikum!

(10)

Dibawah ini contoh coding dengan obyek koneksi OleDbConnection :

Imports System.Data.OleDb

Imports System.Data Public Class frmEntrySP Dim cnn As OleDbConnection

Dim cmmd, cmmd1, cmmd2 As OleDbCommand Dim dReader As OleDbDataReader

Private index As Integer = 0 Private Total As Double = 0.0

Private Sub frmEntrySP_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Buka koneksi

cnn = New OleDbConnection(strConn)

If cnn.State <> ConnectionState.Closed Then cnn.Close() cnn.Open()

'Buat AutoNumber u Surat Pesanan Dim strTemp As String = ""

Dim strValue As String = "" Dim sql As String

sql = "SELECT * FROM SP ORDER BY NOSP DESC" cmmd = New OleDbCommand(sql, cnn) dReader = cmmd.ExecuteReader If dReader.Read Then strTemp = Mid(dReader.Item("NOSP"), 3, 5) Else txtNoSP.Text = "SP00001" Exit Sub End If 'MsgBox(strTemp) strValue = Val(strTemp) + 1

txtNoSP.Text = "SP" & Mid("00000", 1, 5 - strValue.Length) & strValue End Sub

Untuk Textboxt kode supplier :

Private Sub txtNoSP_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNoSP.KeyPress If Asc(e.KeyChar) = 13 Then

btnCariSup.Focus() End If

End Sub

Tombol Cari :

Private Sub btnCariSup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCariSup.Click

Dim popupsup As New PopSup popupsup.ShowDialog() If popupsup.retKdSup <> "" Then txtkdsup.Text = popupsup.retKdSup txtNmSup.Text = popupsup.retNmSup txtNmSup.Focus() End If End Sub

Untuk textboxt nama supplier :

Private Sub txtNmsup_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNmSup.KeyPress If Asc(e.KeyChar) = 13 Then

BtnCariBrg.Focus() End If

(11)

End Sub

Untuk Textboxt Jumlah barang :

Private Sub txtJmlbeli_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtJmlBeli.KeyPress If Asc(e.KeyChar) = 13 Then

SetTotal()

txtTotal.Focus() End If

End Sub

Untuk Textbox Total :

Private Sub txtTotal_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTotal.KeyPress If Asc(e.KeyChar) = 13 Then btnTambah.Focus() End If End Sub Sub SetTotal() Dim a As Integer

a = CInt(txtJmlBeli.Text) * CInt(txtSatuan.Text)

txtTotal.Text = Format(CDbl(CStr(a)), "##,####,####,###") 'txt.Text = Format(CDbl(txt.Text), "##,####,####,###") End Sub

Untuk tombol cari barang :

Private Sub BtnCariBrg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCariBrg.Click

Dim popupbrg As New PopBrg popupbrg.ShowDialog()

If popupbrg.retKdBrg <> "" Then txtKdBrg.Text = popupbrg.retKdBrg txtNmBrg.Text = popupbrg.retNmBrg

txtSatuan.Text = Format(CDbl(popupbrg.retSatuan), "##,####,####,###")

txtJmlBeli.Focus() End If

End Sub

Untuk Tombol tambah :

Private Sub btnTambah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTambah.Click

Dim Value() As String = {txtKdBrg.Text, txtNmBrg.Text, txtSatuan.Text, txtJmlBeli.Text, txtTotal.Text}

AddList(Value)

Total += CDbl(txtTotal.Text) : txtGrandTotal.Text = Format(CDbl(CStr(Total)), "##,####,####,###")

txtKdBrg.Text = "" : txtNmBrg.Text = "" : txtSatuan.Text = "" : txtJmlBeli.Text = ""

txtTotal.Text = "" txtKdBrg.Focus() End Sub

Sub AddList(ByVal Value() As String) Dim subIndex As Integer

ListView1.Items.Add("") For subIndex = 0 To 4 ListView1.Items(index).SubItems.Add("") ListView1.Items(index).SubItems(subIndex).Text = Value(subIndex) Next

(12)

index = index + 1 End Sub

Untuk Tombol Simpan :

Private Sub cmdSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSimpan.Click

Dim counter As Integer Dim sql1, sql2 As String Try

sql1 = "INSERT INTO SP(NOSP, TGLSP, KDSUP) " & _ "VALUES('" & txtNoSP.Text & "','" & _

DateTimePicker3.Text & "','" & _ txtkdsup.Text & "')"

cmmd1 = New OleDbCommand(sql1, cnn) Dim x As Integer = cmmd1.ExecuteNonQuery For counter = 0 To index - 1

sql2 = "INSERT INTO PESAN(TKDBRG,NOSP, JML, HRG) " & _ "VALUES('" & _

ListView1.Items(counter).SubItems(0).Text & "','" & _ txtNoSP.Text & "','" & _

ListView1.Items(counter).SubItems(3).Text & "','" & _ ListView1.Items(counter).SubItems(2).Text & "')" cmmd2 = New OleDbCommand(sql2, cnn)

cmmd2.ExecuteNonQuery() Next

If x = 1 Then

MessageBox.Show("DATA SP BERHASIL DISIMPAN") End If

Catch ex As Exception MsgBox(ex.Message) End Try

End Sub

Untuk tombol Batal :

Private Sub cmdBatal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBatal.Click

ListView1.Items.Clear() index = 0 : Total = 0 txtGrandTotal.Text = "" End Sub

Untuk Tombol keluar :

Private Sub cmdKeluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdKeluar.Click

Me.Close() End Sub End Class

Figur

Memperbarui...

Referensi

Memperbarui...

Related subjects :