Converting Entity Lists to DataTables for Easy Viewing

Viewing a list of entities in the Visual Studio debugger is not easy out of the box. However, Visual Studio makes it easy to browse through a DataTable and look at all its rows and columns because it has a built-in DataTable Visualizer.

So, if you want an easy way to convert a business object's EntityList to a DataTable, add the following method to your ABusinessObject class, so it's inherited by all the business objects in your project:

/// <summary>
/// Converts the EntityList to a DataTable
/// </summary>
/// <returns></returns>
public DataTable ToDataTable()
{
	DataTable table = new DataTable();
 
	// Create Table Columns
	PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(EntityType));
	foreach (PropertyDescriptor prop in properties)
	{
		table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
	}
 
	// Fill Data Rows
	foreach (EntityType item in this.EntityList)
	{
		DataRow row = table.NewRow();
		foreach (PropertyDescriptor prop in properties)
			row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
		table.Rows.Add(row);
	}
	return table;
}

Now you can type the following command in the Visual Studio Watch Window and you can then click the magnifying glass icon to see the converted data in DataTable format.

businessObject.ToDataTable();



© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 07/18/18
Comment or report problem with topic