Fill a datatable from a custom object

Posted on 2012-09-20
Medium Priority
Last Modified: 2012-09-21

I have programmed a custom object that looks something like this:

Public Class MyObject

public property prop1 as string
public property prop2 as integer
public property prop3 as double

public property dr as datarow

And I have created a list(of MyObject)

What i am looking to do is pull from this list, say:
prop1, dr.item("Item_a"), dr.item("Item_j")

(In other words a property or 2 and a few items from the datarow... Not all the items) for each object in the list and to populate that data into a datatable / new custom object (preferably datatable) so i can use it to update something else.

How can I achieve this in the best way?
I'm thinking LINQ, but it's not working for me...

Question by:origfla

Expert Comment

ID: 38419643
What I would do is adding a method to this class which returns an array of object (containing the data for this row... 1 column / array slot).

Then, outside of it, you can use this method to add a new row to the datatable you created.
LVL 83

Accepted Solution

CodeCruiser earned 2000 total points
ID: 38420982
Something like

Dim dTable As New DataTable
dTable.Columns.Add("Prop1", GetType(Integer))
dTable.Columns.Add("Item_A", GetType(String))
dTable.Columns.Add("Item_J", GetType(String))

For Each obj As MyObject in objList
   dTable.Rows.Add(new Object() { obj.Prop1, obj.dr("Item_A"), obj.dr("Item_J") })

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

809 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