8 Replies - 3250 Views - Last Post: 29 March 2012 - 07:38 AM Rate Topic: -----

#1 kampao00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 29-March 12

showing the data by columns in listview.

Posted 29 March 2012 - 03:31 AM

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?

Is This A Good Question/Topic? 0
  • +

Replies To: showing the data by columns in listview.

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: showing the data by columns in listview.

Posted 29 March 2012 - 03:48 AM

You need to add subItems

The way I do this is create a listitem object and add it to the listview
for each myrow in dt.rows
Dim lv as new ListViewItem
Lv.Text = myRow.Item(5)
lv.subitems.add(myrow.item(6) '2nd column
lv.subitems.add(myrow.item(7) '3rd column
...
repeat for all the subitems
ListView.Items.Add(lv)
Next

Was This Post Helpful? 0
  • +
  • -

#3 kampao00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 29-March 12

Re: showing the data by columns in listview.

Posted 29 March 2012 - 04:18 AM

i tried 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
please help me.. :'(
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: showing the data by columns in listview.

Posted 29 March 2012 - 04:26 AM

Post your updated code for this
Was This Post Helpful? 0
  • +
  • -

#5 kampao00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 29-March 12

Re: showing the data by columns in listview.

Posted 29 March 2012 - 04:34 AM


 Public Sub showmyrecords()
        Dim lvi As ListViewItem
        Dim dt As New DataTable
        Dim ds As New DataSet
        ' Dim intCount As Integer

        ds.Tables.Add(dt)

        Dim da As New OleDbDataAdapter("Select * from Schedulings where YearLevels = '" & ComboBox3.Text & "' And Sections = '" & ComboBox1.Text & "' ", asconn)
        da.Fill(dt)
        Dim acscmd As New OleDb.OleDbCommand

        acscmd.CommandText = strsql
        acscmd.Connection = asconn
        dr = acscmd.ExecuteReader()

        For Each myrow__1 As DataRow In dt.Rows
            lvi = New ListViewItem()
            lvi.Text = myrow__1(5)
            lvi.SubItems.Add(myrow__1(6))
            lvi.SubItems.Add(myrow__1(7))
            lvi.SubItems.Add(myrow__1(8))
            lvi.SubItems.Add(myrow__1(9))
            lvi.SubItems.Add(myrow__1(10))
            ListView1.Items.Add(lvi)
        Next
    End Sub




here's the code. kindly help me :) thank you in advance. :)
Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: showing the data by columns in listview.

Posted 29 March 2012 - 05:12 AM

looks ok, other than the executereader part as I don't see any connection open but then you don't need that for what we're doing

So it would appear that you are calling that routine twice. What happens if you add
ListView1.Items.Clear prior to your for each statement? Does it only show it once?
Was This Post Helpful? 0
  • +
  • -

#7 kampao00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 29-March 12

Re: showing the data by columns in listview.

Posted 29 March 2012 - 07:04 AM


Public Sub showmyrecords()
        Dim lvi As ListViewItem
        Dim dt As New DataTable
        Dim ds As New DataSet
        ' Dim intCount As Integer

        ds.Tables.Add(dt)

        Dim da As New OleDbDataAdapter("Select * from Schedulings where YearLevels = '" & ComboBox3.Text & "' And Sections = '" & ComboBox1.Text & "' ", asconn)
        da.Fill(dt)
        Dim acscmd As New OleDb.OleDbCommand

        acscmd.CommandText = strsql
        acscmd.Connection = asconn

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

    End Sub




ahm .. here's my edited code.. but here's no output in my listview.. how should i do it? or am i on wrong place of codes of listview1.items.clear() ?
Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: showing the data by columns in listview.

Posted 29 March 2012 - 07:28 AM

Now you are clearing the list after you populate it. You need to move that line up to line 15 where you will clear the list and then fill it with the items from the datatable.
Was This Post Helpful? 0
  • +
  • -

#9 kampao00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 29-March 12

Re: showing the data by columns in listview.

Posted 29 March 2012 - 07:38 AM

yes! at last.! it produce right output! thankkk youuuuuuu!
god bless.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1