Solved

How do I Populate a ListView with the contents of a Dataset?

Posted on 2006-06-28
3
238 Views
Last Modified: 2006-11-18
Hi,

In my app I currently have a Dataset with one table full of data. I need to be able to programmatically transfer all that data to a listview. As the number of rows and columns in the dataset may change I need the code to be able to cope with this too. Any ideas?

Thanks.
0
Comment
Question by:kiranboi
  • 2
3 Comments
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 17000001

        'I am creating a dummy DataTable here you might have your own code to get
        'the dataTable filled
        Dim DT As New DataTable
        DT.Columns.Add("Name", GetType(String))
        DT.Columns.Add("Age", GetType(Integer))
        DT.Columns.Add("Address", GetType(String))
        Dim Dr As DataRow
        For i As Integer = 1 To 5
            Dr = DT.NewRow
            Dr(0) = "Name " & CStr(i)
            Dr(1) = 10 * i
            Dr(2) = "Address " & CStr(i)
            DT.Rows.Add(Dr)
        Next


        '---Code to show data in ListView from the DataTable
        'Since we are not adding images the view woould be Details type
        LstView.View = View.Details
        'Now Add Columns and Headers to the List View
        For Each DC As DataColumn In DT.Columns
            LstView.Columns.Add(DC.ColumnName)
        Next
        'Now Add the Rows
        Dim Itm As ListViewItem
        For Each Dr In DT.Rows
            Itm = New ListViewItem(Dr(0).ToString)
            Itm.SubItems.Add(Dr(1))
            Itm.SubItems.Add(Dr(2))
            LstView.Items.Add(Itm)
        Next
0
 

Author Comment

by:kiranboi
ID: 17000027
Thats looking like the sort of thing i need. but instead of a datatable i have a dataset populated from an excel sheet. any chance of showing me the code i would need to use that. Thanks
0
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 500 total points
ID: 17000044
access the DataTable inside the Dataset like this YouDataSet.Tables(0)
so the code would change to

        LstView.View = View.Details
        For Each DC As DataColumn In YouDataSet.Tables(0).Columns '<-- Change the Dataset name here
            LstView.Columns.Add(DC.ColumnName)
        Next

        Dim Itm As ListViewItem
        For Each Dr In YouDataSet.Tables(0).Rows   '<-- Dataset name here
            Itm = New ListViewItem(Dr(0).ToString)
            Itm.SubItems.Add(Dr(1))
            Itm.SubItems.Add(Dr(2))
            LstView.Items.Add(Itm)
        Next
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VS.net 2010 11 35
Help with error when uploading excel file 3 28
Write to a printer using vb.net 9 27
How to open form using item in Listbox. 8 21
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question