Skip to content

3. Working with columns

Tom Wright edited this page Feb 2, 2018 · 1 revision

ConTabs is designed to work with IEnumerables of your existing data types. Since your existing data types probably won't have been designed specifically for tabular output, ConTabs provides options for hiding certain columns and for reordering those that you do want to show.

Controlling columns

Columns can be accessed by name or by index.

  • When addressing a column by name, the list of columns will first be searched by the name of the underlying property (PropertyName). If a column can't be matched using PropertyName, the ColumnName property (which can be customised, see below) will be used.

  • When addressing a column by index, be aware that the index will change when columns are re-ordered.

Re-naming columns

If you want to change the header for a column, this can be achieved by setting the ColumnName property.

Starting with a table like this:

╔═════════╦═════════════════╦═══════════════╗
║ Name    ║ DistanceFromSun ║ OrbitalPeriod ║
╠═════════╬═════════════════╬═══════════════╣
║ Mercury ║ 57909227        ║ 88            ║
║ Venus   ║ 108209475       ║ 225           ║
║ Earth   ║ 149598262       ║ 365.24        ║
╚═════════╩═════════════════╩═══════════════╝

We can do this:

table.Columns["OrbitalPeriod"].ColumnName = "New name";

And we get this:

╔═════════╦═════════════════╦══════════╗
║ Name    ║ DistanceFromSun ║ New name ║
╠═════════╬═════════════════╬══════════╣
║ Mercury ║ 57909227        ║ 88       ║
║ Venus   ║ 108209475       ║ 225      ║
║ Earth   ║ 149598262       ║ 365.24   ║
╚═════════╩═════════════════╩══════════╝

Hiding columns

Hiding a column is as simple as setting the Hide property to true. If we start with this table:

╔═════════╦═════════════════╦═══════════════╗
║ Name    ║ DistanceFromSun ║ OrbitalPeriod ║
╠═════════╬═════════════════╬═══════════════╣
║ Mercury ║ 57909227        ║ 88            ║
║ Venus   ║ 108209475       ║ 225           ║
║ Earth   ║ 149598262       ║ 365.24        ║
╚═════════╩═════════════════╩═══════════════╝

And do this:

table.Columns["OrbitalPeriod"].Hide = true;

We get this:

╔═════════╦═════════════════╗
║ Name    ║ DistanceFromSun ║
╠═════════╬═════════════════╣
║ Mercury ║ 57909227        ║
║ Venus   ║ 108209475       ║
║ Earth   ║ 149598262       ║
╚═════════╩═════════════════╝

Re-ordering columns

Changing the order in which the columns are output is a little different, but still really easy. Rather than acting on the column itself, we use the MoveColumn method in Columns.

Starting with our standard table:

╔═════════╦═════════════════╦═══════════════╗
║ Name    ║ DistanceFromSun ║ OrbitalPeriod ║
╠═════════╬═════════════════╬═══════════════╣
║ Mercury ║ 57909227        ║ 88            ║
║ Venus   ║ 108209475       ║ 225           ║
║ Earth   ║ 149598262       ║ 365.24        ║
╚═════════╩═════════════════╩═══════════════╝

We can do this:

table.Columns.MoveColumn("OrbitalPeriod", 1);

And see that the "OrbitalPeriod" column has moved to position 1, pushing DistanceFromSun into position 2:

╔═════════╦═══════════════╦═════════════════╗
║ Name    ║ OrbitalPeriod ║ DistanceFromSun ║
╠═════════╬═══════════════╬═════════════════╣
║ Mercury ║ 88            ║ 57909227        ║
║ Venus   ║ 225           ║ 108209475       ║
║ Earth   ║ 365.24        ║ 149598262       ║
╚═════════╩═══════════════╩═════════════════╝

Just like the other column methods, MoveColumn can take either the name or the index of the column you want to move, so the following is also valid:

table.Columns.MoveColumn(3, 1);