Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Create a datatable from generic list

Posted on 2011-09-16
2
Medium Priority
?
397 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

772 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