0

Hi i am new to C# and wondering if there is something that exists to simplify what i am trying to do. I am using this sample to create a datatable from a class. Using Reflection to create a DataTable from a Class?

Question:

i would like to add rows to my datatable without individually populating each col. How would you add a row to this datatable from an object?

Sample:

DataTable dogTable = CreateDataTable(typeof(Dog));
grid.datasource = dogTable;
...
private void populateData(Dog newDog)
{
   DataRow row;
   ...
   //How do i populate without without going through each field ?? possible ?
   //Since datatable was created from Dog there is a one to one relationship betwee
   //datatable and dog class.

   dogTable.Rows.Add(row);
}

Is there an easy way to put newDog in the table ?

3
  • Read about Entity Framework - particularly Code First... Commented Mar 26, 2014 at 15:15
  • Why do you want to create a DataTable from a single object? That would always give a single DataRow. Or do you want to have a single DataColumn (f.e. PropertyName) and one row for each property? Commented Mar 26, 2014 at 15:21
  • maybe i didnt think this through. using the sample i am trying to create a table that contains columns that match properties of Dog. Object of Dog will then be each row in that table. Does that make sense ? Commented Mar 27, 2014 at 15:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.