To convert a class instance to a DataRow
public DataRow ConvertClassToDataRow(ClassName theClass)
{
DataTable table = new DataTable();
DataRow row = table.NewRow();
PropertyInfo[] properties = theClass.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
DataColumn col = new DataColumn(property.Name, property.PropertyType);
table.Columns.Add(col); row[property.Name] = property.GetValue(theClass, null);
}
return row;
}
No comments:
Post a Comment