Solved

Create a datatable from generic list

Posted on 2011-09-16
2
392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help with a query 14 48
Need help with another query 10 50
.NET Redemption/Outlook Examples 3 24
.NET universe documentation poster 2 16
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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 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