List View Control Visual Basic .Net

ANDA bisa menggunakan Listview control di visual basic untuk menampung data selain menggunakan DataGridView
Berikut adalah kode pada control Listview yang digunakan untuk menampung data yang diambil dari database Microsoft Access.
Imports System.Data.OleDb

Public Class Form1

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

       
Dim sConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\dataku.accdb;Persist Security Info=False;"

       
'-- set kolom
        ListView1.View = View.Details
        ListView1.Columns.Add("nim", 50, HorizontalAlignment.Left)
        ListView1.Columns.Add("nama", 100, HorizontalAlignment.Left)
        ListView1.Columns.Add("jurusan", 200, HorizontalAlignment.Left)

       
'-- ambil data
        Dim oDa As New OleDb.OleDbDataAdapter("select * from mahasiswa", sConn)
        Dim oTbl As New DataTable
        oDa.Fill(oTbl)

       
Dim oDr As DataRow
        Dim ol As ListViewItem
       
ListView1.Items.Clear()
        Dim i As Integer

       
For i = 0 To oTbl.Rows.Count - 1
            oDr = oTbl.Rows(i)
            ol = ListView1.Items.Add(oDr.Item("nim"))
            ol.SubItems.Add(oDr("nama"))
            ol.SubItems.Add(oDr("jurusan"))
        Next i
   
End Sub

    Private Sub btnPilih_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPilih.Click

       
'-- pilih data
        MsgBox(ListView1.SelectedItems.Item(0).Text & "|" & _
               ListView1.SelectedItems.Item(0).SubItems(1).Text & "|" & _
               ListView1.SelectedItems.Item(0).SubItems(2).Text)
   
End Sub

End Class

Berikut adalah demo programnya:

0 komentar:

Poskan Komentar

Starcraft Pointer
 
Web Design by : Givo-Alfajri ..!! ®