hi :D

please help me with this problem. i'm a newbie in vb.net

i have 6 columns in my listview that should show the data from the database.. but when i coded it like this :

 Public Sub showmyrecords()
    Dim dt As New DataTable
    Dim ds As New DataSet
    ds.Tables.Add(dt)
    Dim da As New OleDbDataAdapter("Select * from Schedulings where YearLevels = '" & ComboBox3.Text & "', Sections = '" & ComboBox1.Text & "' ", asconn)
    da.Fill(dt)
    Dim myrow As DataRow
    For Each myrow In dt.Rows
        ListView1.Items.Add(myrow.Item(5))
        ListView1.Items.Add(myrow.Item(6))
        ListView1.Items.Add(myrow.Item(7))
        ListView1.Items.Add(myrow.Item(8))
        ListView1.Items.Add(myrow.Item(9))
        ListView1.Items.Add(myrow.Item(10))

    Next
End Sub

it shows the first row data(database) in the first column of my listview... how should i solve this problem?

Recommended Answers

All 4 Replies

The ListView control is sometimes very smart.
Create a String array, and add your information to that.
And then you create a new ListViewItem and assign the array to it.
Last, add the ListViewItem to the ListView.

Dim str(6) As String
Dim dt As New DataTable
Dim da As New OleDbDataAdapter("SELECT * FROM Schedulings WHERE YearLevels = '" & ComboBox3.Text & "', Sections = '" & Combobox1.Text & "'", asconn)
da.Fill(dt)

For Each myrow As DataRow In dt.Rows
    str(0) = myrow.Item(5)
    str(1) = myrow.Item(6)
    str(2) = myrow.Item(7)
    str(3) = myrow.Item(8)
    str(4) = myrow.Item(9)
    str(5) = myrow.Item(10)
Next

Dim item As New ListViewItem(str)
ListView1.Items.Add(item)

If

there are multiple columns i datatable, you have to use Subitems to fill listView (item in only 1st column in listview, else are subitems).
So you should do:

Dim lvi As ListViewItem
For Each myrow__1 As DataRow In dt.Rows
    lvi = New ListViewItem()
    lvi.Text = mRyow(5).ToString()
    lvi.SubItems.Add(myRow(6).ToString())
    '6 is 7th column in datatable!!!
    lvi.SubItems.Add(myRow(7).ToString())
    'and so on...
    lvi.SubItems.Add(myRow(8).ToString())
    lvi.SubItems.Add(myRow(9).ToString())
    lvi.SubItems.Add(myRow(10).ToString())
    ListView1.Items.Add(lvi)
Next

@oxiegen i have tried your codes but it only produce the last row record and double it in my listview

@mitja i tried also your codes . yes it produce the all right data that answers my query but it also double it in my list view.

in example :

if theres a 6 records in my database , it produce 6x in listview with the same data. :O

how could i solve these problems? thank you in advance. :)

I see the mistake I made.
Move the last two lines so that they are inside the For loop.

But also, before the For loop. Add this line ListView1.Items.Clear()

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.