2020年5月7日 星期四

VB.NET Access顯示在ListView [Show Access Data in Listview]


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Imports System.Data

Imports System.Data.OleDb



    Dim str As String

    Dim conn As New OleDbConnection

    Dim ds As New DataTable

    Dim adp As New OleDbDataAdapter

    Dim cmd As New OleDbCommand


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

        str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\DataBase.mdb;Jet OLEDB:Database Password=8AFE2B7801749F02C649"

        conn = New OleDbConnection(str) '創建連線物件


        Try

            conn.Open() '打開連接

            'MessageBox.Show("已經正確建立連接!", "連接正確")

        Catch ex As Exception

            MessageBox.Show(ex.Message, "連接錯誤")

            Exit Sub

        End Try



        adp = New OleDbDataAdapter("select * from Record", conn)

        adp.Fill(ds)


        ShowDataInLvw(ds, ListView1)

        'MsgBox(ds.Rows(0).Item(0).ToString)

        'MsgBox(ds.Rows(0).Item("VecesError").ToString)

        DataGridView1.DataSource = ds

        conn.Close()

    End Sub


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    Private Sub ShowDataInLvw(ByVal data As DataTable, ByVal lvw As ListView)

        lvw.View = View.Details

        lvw.GridLines = True

        lvw.Columns.Clear()

        lvw.Items.Clear()

        For Each col As DataColumn In data.Columns

            lvw.Columns.Add(col.ToString, (col.ToString.Length * 15))

        Next


        For Each row As DataRow In data.Rows

            Dim lst As ListViewItem

            lst = lvw.Items.Add(row(0))

            For i As Integer = 1 To data.Columns.Count - 1

                lst.SubItems.Add(row(i))

            Next

        Next

    End Sub

沒有留言:

張貼留言