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 ?

Community
  • 1
  • 1
kaio123
  • 1
  • 1
  • Read about Entity Framework - particularly Code First... – Martin Milan Mar 26 '14 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? – Tim Schmelter Mar 26 '14 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 ? – kaio123 Mar 27 '14 at 15:46

0 Answers0