hi!!
im a new to vb.net and im having a problem, pls help me

ok here it is...

i created a listview, and i have a button5 "view records"
if i click the button, all the records should be shown to the listview but yeah i doesn't work

here are may codes:

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        conn = New OleDbConnection("Provider = Microsoft.ace.oledb.12.0; data source=|DataDirectory|\AutomatedLibrarySystem.accdb")
        conn.Open()
        str = "select * from BookInfo"
        com = New OleDbCommand(str, conn)
        oledbda = New OleDbDataAdapter(com)
        ds = New DataSet
        oledbda.Fill(ds, "BookInfo")
        ListView1.Clear()
        dt = ds.Tables("BookInfo")
        Dim i As Integer
        For i = 0 To dt.Rows.Count - 1
            ListView1.Items.Add(dt.Rows(i).ItemArray(1))
            ListView1.Items(i).SubItems.Add(dt.Rows(i).ItemArray(2))
            ListView1.Items(i).SubItems.Add(dt.Rows(i).ItemArray(3))
            ListView1.Items(i).SubItems.Add(dt.Rows(i).ItemArray(4))
            ListView1.Items(i).SubItems.Add(dt.Rows(i).ItemArray(5))
            ListView1.Items(i).SubItems.Add(dt.Rows(i).ItemArray(6))
            ListView1.Items(i).SubItems.Add(dt.Rows(i).ItemArray(7))
            ListView1.Items(i).SubItems.Add(dt.Rows(i).ItemArray(8))
            ListView1.Items(i).SubItems.Add(dt.Rows(i).ItemArray(9))
            ListView1.Items(i).SubItems.Add(dt.Rows(i).ItemArray(10))
        Next
        conn.Close()
    End Sub

thank you =D

Recommended Answers

All 5 Replies

Try it this way:

' 1. create columns ot listView!!
' 2. then loop through the row and columns to datatable:
' NOTE: make sure that the number of columns in listView IS equal to columns to datatable!!

For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
    Dim lvi As New ListViewItem()
    For j As Integer = 0 To ds.Tables(0).Rows(i).ItemArray.Length - 1
        lvi.SubItems.Add(ds.Tables(0).Rows(i)(j).ToString())
    Next
    listView1.Items.Add(lvi)
Next
commented: thank you~ =) +0
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        conn = New OleDbConnection("Provider = Microsoft.ace.oledb.12.0; data source=|DataDirectory|\AutomatedLibrarySystem.accdb")
        conn.Open()
        str = "select * from BookInfo"
        com = New OleDbCommand(str, conn)
        oledbda = New OleDbDataAdapter(com)
        ds = New DataSet
        oledbda.Fill(ds, "BookInfo")
        ListView1.Clear()
        dt = ds.Tables("BookInfo")

        For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
            Dim lvi As New ListViewItem()
            For j As Integer = 0 To ds.Tables(0).Rows(i).ItemArray.Length - 1
                lvi.SubItems.Add(ds.Tables(0).Rows(i)(j).ToString())
            Next
            ListView1.Items.Add(lvi)
        Next

        conn.Close()
    End Sub

like this?
I'll sure that the columns in listview is equaal to the columns in datatable
it doesn't work
:(

Sure it doesnt.
Why you called listView1.Clear() method? This clears all the data and columns.
Do only:

listView1.Items.Clear()

yes!!! it works =D

thank you @Mitja Bonca

I am glad it did

vote +1 and comment now :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.