Solved

Create a datatable from generic list

Posted on 2011-09-16
2
388 Views
Last Modified: 2012-05-12
I have a function that expects an object.
This object is a List(Of ) type of object.
I can't make reference to the original list because it varies all the time (it is not always the same)
I am trying to convert the colection into a datatable.
So far, I was able to create the columns using the name of the properties in the object.
I am now trying to dump the content of the list into the table by looping through the list and getting the values from the properties but I am getting the following error


Object does not match target type.

This errors happens in this line:

newRow(prop.Name) = prop.GetValue(listOfData, Nothing)

Any ideas how can I do this?

Thanks in advanced,





The code below is my funciton but I
Function populateViewFromListOf(ByVal listOfData As Object) As DataView
        'Create a temp datatable
        Dim dataListTable As DataTable = New DataTable("GridDataTable")

        'Get the properties from the list/collection
        'and Loop through them to get the definition and add them to the datatable
        For Each pi As PropertyInfo In listOfData.GetType().GetProperties()
            Dim properties As PropertyInfo()
            properties = pi.PropertyType.GetProperties
            For Each prop As PropertyInfo In properties
                dataListTable.Columns.Add(prop.Name, prop.PropertyType)
            Next
        Next

        'Loop through the list and add each item to the rows collection
        For Int As Integer = 0 To listOfData.Count - 1
            Dim newRow As DataRow = dataListTable.NewRow()
            For Each pi As PropertyInfo In listOfData.Item(Int).GetType().GetProperties()
                Dim properties As PropertyInfo()
                properties = pi.PropertyType.GetProperties
                For Each prop As PropertyInfo In properties
                    newRow(prop.Name) = prop.GetValue(listOfData, Nothing)  -->THIS IS THE LINE THAT BREAKS !!!!
                Next
            Next
            dataListTable.Rows.Add(newRow)
        Next


        'return the table view
        Return dataListTable.DefaultView
    End Function

Open in new window

0
Comment
Question by:melinalt
2 Comments
 
LVL 15

Accepted Solution

by:
x77 earned 500 total points
ID: 36550692
Note that you get names from Gettype from Element in List:

    Dim type = listOfData(0).GetType
    Dim properties = type.GetProperties

Do´nt use "GetProperties" twice

You get value from Element in list, not from List:

      prop.GetValue(Dat, Nothing)
Dim type = listOfData(0).GetType
        Dim properties = type.GetProperties
        For Each prop As PropertyInfo In properties
            dataListTable.Columns.Add(prop.Name, prop.PropertyType)
        Next

        For Each Dat In listOfData
            Dim newRow = dataListTable.NewRow
            For Each prop As PropertyInfo In properties
                newRow(prop.Name) = prop.GetValue(Dat, Nothing) '  -->THIS IS THE LINE THAT BREAKS !!!!
            Next
            dataListTable.Rows.Add(newRow)
        Next

Open in new window

0
 
LVL 1

Author Closing Comment

by:melinalt
ID: 36550771
Your solution works perfect
Thanks a lot
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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

22 Experts available now in Live!

Get 1:1 Help Now