Solved

Create a datatable from generic list

Posted on 2011-09-16
2
390 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 (3.5 Framework) Remove all items from List 3 22
Regex validation 2 24
performance issue in asp web 5 32
Complex SQL statement in VB.NET 7 15
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

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