create datatable

  • Thread starter Thread starter Raymond Chiu
  • Start date Start date
R

Raymond Chiu

If I have created the data class, how can it be used in creating datatable
with looping data class field?
e.g.
public class PriceData
{
public string Code { get; set; }
public string Description { get; set; }
public string Currency_Code { get; set; }
public decimal Unit_Price { get; set; }
}
..........................................
DataTable dt=new datatable();
while (looping all price data field)
dt.columns.add( price data field[index]);
loop

What the code should be???
 
Raymond Chiu said:
If I have created the data class, how can it be used in creating datatable
with looping data class field?
e.g.
public class PriceData
{
public string Code { get; set; }
public string Description { get; set; }
public string Currency_Code { get; set; }
public decimal Unit_Price { get; set; }
}
.........................................
DataTable dt=new datatable();
while (looping all price data field)
dt.columns.add( price data field[index]);
loop

What the code should be???

If you want to extract the field information from the class at runtime,
you will need to use Reflection:

using System.Reflection;
....
Type t = typeof(PriceData);
PropertyInfo[] props = t.GetProperties();
DataTable dt=new DataTable();
foreach (PropertyInfo prop in props)
{
dt.Columns.Add(prop.Name, prop.PropertyType);
}
 
Back
Top