Solved

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

Posted on 2006-06-28
3
234 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

943 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now