Results 1 to 3 of 3

Thread: [RESOLVED] Adding to listview from access database(vs 2008)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Location
    London, England
    Posts
    142

    Resolved [RESOLVED] Adding to listview from access database(vs 2008)

    My problem is very simple, i have 4 columns at the moment, this code is supposed to add the column name, and then the details to a listview. Sounds easy, but what i am getting is that all the row details are being listed in column 1. Can anyone tell me why the rows are not been added to the right column?

    Code:
    For i = 0 To ds.Tables("Contacts").Columns.Count - 1 'Loops until all columns have been added
    
                'Stores column name as string
                strColumnName = ds.Tables("Contacts").Columns.Item(i).ColumnName
                'Skip over column ID
                If strColumnName = "ID" Then
    
                Else
                    'Add Column name to listview
                    lv1.Columns.Add(strColumnName)
    
                    'Loop until rows have been added
                    For j = 0 To ds.Tables("Contacts").Rows.Count - 1
                        'Add Row name to string
                        strRowName = ds.Tables("Contacts").Rows(j).Item(strColumnName).ToString
    
                        'Add string to listview
                        lv1.Items.Add(strRowName)
                    Next
    
                    lv1.Update()
                End If
    Thanks in advance

    Redmo
    The universal aptitude for ineptitude makes any human accomplishment an incredible miracle -Col. John P. Stapp


    Please rate the posts that have helped. Makes us feel all warm and fuzzy

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Adding to listview from access database(vs 2008)

    You keep adding items to the ListView. At no point do you add any subitems to an item, which is how you populate columns other than the first. Your code should look something like this:
    vb.net Code:
    1. Private Sub PopulateListView(ByVal list As ListView, ByVal table As DataTable)
    2.     list.BeginUpdate()
    3.  
    4.     For Each column As DataColumn In table.Columns
    5.         list.Columns.Add(column.ColumnName)
    6.     Next
    7.  
    8.     Dim upperBound As Integer = table.Columns.Count - 1
    9.     Dim item As ListViewItem
    10.  
    11.     For Each row As DataRow In table.Rows
    12.         item = New ListViewItem(row(0).ToString())
    13.  
    14.         For index As Integer = 1 To upperBound
    15.             item.SubItems.Add(row(index).ToString())
    16.         Next
    17.  
    18.         item.Tag = row
    19.         list.Items.Add(item)
    20.     Next
    21.  
    22.     list.EndUpdate()
    23. End Sub
    Note that I store the corresponding DataRow in each ListViewItem's Tag property. This is unnecessary if you don't need access to the row from the item but doesn't hurt.

    By the way, this question has nothing to do with databases and therefore belongs in the VB.NET forum. The fact that the data in the DataTable comes from a database is irrelevant. A DataTable is a DataTable, wherever the data came from. It's with the ListView you're having the problem and that's a purely VB issue.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Location
    London, England
    Posts
    142

    Re: Adding to listview from access database(vs 2008)

    Thanks jmcilhinney. Your a genius. Code worked a treat. Sorry about posting in the wrong area. At the time of the posting i was convinced that i was reading from the datatable wrong.

    Redmo
    The universal aptitude for ineptitude makes any human accomplishment an incredible miracle -Col. John P. Stapp


    Please rate the posts that have helped. Makes us feel all warm and fuzzy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width