?
Solved

Create a datatable from generic list

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

801 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