Membuat Grafik Berdasarkan Tabel Database VB

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Membuat Grafik berdasarkan Tabel database VB.

Net

pertama buat form seperti berikut yang terdapat satu DataGridView dan Chart, atau
menyesuaikan dengan project yang kalian buat.

Tambahkan Modul untuk koneksi, berikut codingnya :

Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms.DataVisualization.Charting
Module MODULKONEKSI
Public cmd As New SqlCommand
Public dttable As New DataTable
Public SQL1 As String
Public dtadapter As SqlDataAdapter = Nothing
Public koneksi As String
Public conn As New SqlConnection

'-------------------Koneksi ke database-----------
Sub konek()
koneksi = "Data Source=MDBK-PC\SQLEXPRESS;Initial
Catalog=JABAL_IT_CONSULTANT;Integrated Security=true"
conn = New SqlConnection(koneksi)
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
End Sub

'----------Fungsi untuk menentukan data yang akan ditampilkan digrafik

Public Sub GRAFIK_STOKBARANG(ByVal chart1 As Chart)


Try
Dim cnt As New SqlClient.SqlConnection(koneksi)
Dim cmd As New SqlClient.SqlCommand
cnt.Open()
cmd.Connection = cnt
Dim Comment As String = "SELECT * FROM BARANG"
Dim da As New SqlClient.SqlDataAdapter(Comment, cnt)
Dim ds As New DataSet()
da.Fill(ds, "grafik")
chart1.Series("Ket").XValueMember = "Nama"
chart1.Series("Ket").YValueMembers = "JUMLAH"
chart1.DataSource = ds.Tables("grafik")
cnt.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Module

Kemudian berikut coding untuk form grafik :

Imports System.Data.SqlClient
Public Class FGrafik
Sub daftar()
Call konek()
Dim sql2 As String
sql2 = "select kode,nama,jumlah from Barang order by Nama asc"
dtadapter = New SqlDataAdapter(sql2, conn)
DataGridView1.Refresh()
Dim AGT As New DataTable
AGT.Clear()
dtadapter.Fill(AGT)
DataGridView1.DataSource = AGT
conn.Close()
End Sub

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


Handles MyBase.Load
daftar()
GRAFIK_STOKBARANG(Chart1)
End Sub
End Class

Selamat mencoba, dan semoga bermanfaat.

You might also like