Creating Tables from Various Data Sources

ConsoleTables is designed to be flexible, allowing you to create tables from several common data structures in .NET.

1. Manual Creation

The most direct way to create a table is by defining the columns and adding rows manually. This gives you full control over the table's content.

var table = new ConsoleTable("one", "two", "three");
table.AddRow(1, 2, 3)
     .AddRow("this line should be longer", "yes it is", "oh");

table.Write();

Note: The number of items in AddRow must exactly match the number of columns defined in the constructor, otherwise an exception will be thrown.

2. From a Collection of Objects (From<T>)

A powerful feature of ConsoleTables is its ability to generate a table directly from an IEnumerable<T>. The library uses reflection to read the public properties of the type T and uses their names as column headers.

public class User
{
    public string Name { get; set; }
    public int Age { get; set; }
}

var users = new List<User>
{
    new User { Name = "Alexandre", Age = 36 },
    new User { Name = "René", Age = 59 },
};

ConsoleTable.From(users).Write();

Output:

 ------------------- 
 | Name      | Age |
 ------------------- 
 | Alexandre | 36  |
 ------------------- 
 | René      | 59  |
 ------------------- 

 Count: 2

3. From a DataTable

If you are working with data from a database or other sources that use System.Data.DataTable, you can easily convert it into a ConsoleTable.

using System.Data;

DataTable data = new DataTable();
data.Columns.Add("A", typeof(bool));
data.Columns.Add("B", typeof(bool));
data.Columns.Add("C", typeof(bool));
data.Rows.Add(true, false, true);
data.Rows.Add(false, true, false);

var table = ConsoleTable.From(data);
table.Write();

4. From a Dictionary

You can create a table from a nested dictionary structure, specifically Dictionary<string, Dictionary<string, object>>. The outer dictionary's keys become the first column, and the inner dictionaries' keys are pivoted to become the column headers.

var data = new Dictionary<string, Dictionary<string, object>>
{
    {"A", new Dictionary<string, object> { { "A", true }, { "B", false }, { "C", true } } },
    {"B", new Dictionary<string, object> { { "A", false }, { "B", true }, { "C", false } } },
    {"C", new Dictionary<string, object> { { "A", false }, { "B", false }, { "C", true } } }
};

var table = ConsoleTable.FromDictionary(data);
table.Write();

Output:

 --------------------------------- 
 |   | A     | B     | C     |
 --------------------------------- 
 | A | True  | False | True  |
 --------------------------------- 
 | B | False | True  | False |
 --------------------------------- 
 | C | False | False | True  |
 --------------------------------- 

 Count: 3

5. From a 2D Object Array (Object[][])

You can also generate a table from a jagged array of objects. By convention, the first inner array is treated as the header row, and all subsequent arrays are treated as data rows.

Object[][] userData =
[
    ["Column1", "Column2"],
    ["Test", 1],
    ["Test", 2]
];

var table = ConsoleTable.From(userData);
table.Write();