Menampilkan data dengan Data Grid View
Kali ini, kita akan coba buat data sederhana yang dapat ditampilkan secara langsung pada DataGridView.
Langkah-langkah untuk membuat Quick Sort di VB.Net adalah:
1. Buat sebuah project baru di VB.Net. Tambahkan kontrol DataGridView dengan nama DataGridView1, dan tiga buah commandbuttonyangmasing-masing bernama Button1 (dengan caption Load Data), Button 2 (dengan caption Clear Data), dan Button 3 (dengan caption Exit).
2. Kode berikut digunakan untuk mengisikan data yang hendak ditampilkan. Data yang ditampilkan menggunakan class TableData yang terdiri dari 3 property, yaitu Pemilik, Peliharaan, dan NamaPeliharaan. Masing-masing property berjenis string:
Public Class Form1
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Dim td1 As New TableData
td1.Pemilik = "Frank"
td1.Peliharaan = "Sapi"
td1.NamaPeliharaan = "Mooow"
Dim td2 As New TableData
td2.Pemilik = "Goldi"
td2.Peliharaan = "Anjing"
td2.NamaPeliharaan = "Bleki"
Dim td3 As New TableData
td3.Pemilik = "Karlo"
td3.Peliharaan = "Katak"
td3.NamaPeliharaan = "Kwok-kwok"
Dim arr(3) As TableData
Dim i As Long
arr(0) = td1
arr(1) = td2
arr(2) = td3
DataGridView1.DataSource = arr
For i = 0 To 2
'autoresize each column
DataGridView1.AutoResizeColumn(i)
Next i
End Sub
Private Sub Button3_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles Button3.Click
End
End Sub
Private Sub Button2_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
DataGridView1.DataSource = Nothing
End Sub
End Class
3. Struktur data untuk data yang hendak ditampilkan, disimpan pada Class TableData:
Public Class TableData
Private mOwner As String
Private mPet As String
Private mPetname As String
Public Property Pemilik() As String
Get
Return mOwner
End Get
Set(ByVal value As String)
mOwner = value
End Set
End Property
Public Property Peliharaan() As String
Get
Return mPet
End Get
Set(ByVal value As String)
mPet = value
End Set
End Property
Public Property NamaPeliharaan() As String
Get
Return mPetname
End Get
Set(ByVal value As String)
mPetname = value
End Set
End Property
End Class
4. Jalankan program, dan hasil yang ditampilkan adalah:
Public Class Form1
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Dim td1 As New TableData
td1.Pemilik = "Frank"
td1.Peliharaan = "Sapi"
td1.NamaPeliharaan = "Mooow"
Dim td2 As New TableData
td2.Pemilik = "Goldi"
td2.Peliharaan = "Anjing"
td2.NamaPeliharaan = "Bleki"
Dim td3 As New TableData
td3.Pemilik = "Karlo"
td3.Peliharaan = "Katak"
td3.NamaPeliharaan = "Kwok-kwok"
Dim arr(3) As TableData
Dim i As Long
arr(0) = td1
arr(1) = td2
arr(2) = td3
DataGridView1.DataSource = arr
For i = 0 To 2
'autoresize each column
DataGridView1.AutoResizeColumn(i)
Next i
End Sub
Private Sub Button3_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles Button3.Click
End
End Sub
Private Sub Button2_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
DataGridView1.DataSource = Nothing
End Sub
End Class
3. Struktur data untuk data yang hendak ditampilkan, disimpan pada Class TableData:
Public Class TableData
Private mOwner As String
Private mPet As String
Private mPetname As String
Public Property Pemilik() As String
Get
Return mOwner
End Get
Set(ByVal value As String)
mOwner = value
End Set
End Property
Public Property Peliharaan() As String
Get
Return mPet
End Get
Set(ByVal value As String)
mPet = value
End Set
End Property
Public Property NamaPeliharaan() As String
Get
Return mPetname
End Get
Set(ByVal value As String)
mPetname = value
End Set
End Property
End Class
4. Jalankan program, dan hasil yang ditampilkan adalah:
Ohya, baca juga artikel: Nissan X-trail Mobil SUV Tangguh dan Sporty Terbaik
1 komentar:
thanks mas..lage belajar VB dot net jg..:D
Posting Komentar