Solved

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

Posted on 2006-06-28
3
230 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
Comment Utility

        '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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
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…

762 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

9 Experts available now in Live!

Get 1:1 Help Now