-
Notifications
You must be signed in to change notification settings - Fork 19
4. Format strings and alignment
Numeric content in particular may look a bit odd in a table, if the default settings are used. ConTabs has two features that can help make your numbers (or dates) look a bit more natural: format strings and alignment settings.
If the underlying type for a column has a ToString()
method that takes a format string, a format string can be applied to the column by setting the FormatString
property. Whatever this is set to will be passed down to the ToString()
method, which means all the standard and custom formatting options are available.
So, if we start with our table of planets:
╔═════════╦═════════════════╦═══════════════╗
║ Name ║ DistanceFromSun ║ OrbitalPeriod ║
╠═════════╬═════════════════╬═══════════════╣
║ Mercury ║ 57909227 ║ 88 ║
║ Venus ║ 108209475 ║ 225 ║
║ Earth ║ 149598262 ║ 365.24 ║
╚═════════╩═════════════════╩═══════════════╝
We can do this:
table.Columns["DistanceFromSun"].FormatString = "E3";
table.Columns["OrbitalPeriod"].FormatString = "0.0 days";
Which makes our table look like this:
╔═════════╦═════════════════╦═══════════════╗
║ Name ║ DistanceFromSun ║ OrbitalPeriod ║
╠═════════╬═════════════════╬═══════════════╣
║ Mercury ║ 5.791E+007 ║ 88.0 days ║
║ Venus ║ 1.082E+008 ║ 225.0 days ║
║ Earth ║ 1.496E+008 ║ 365.2 days ║
╚═════════╩═════════════════╩═══════════════╝
Here we've told "DistanceFromSun" to use the standard "scientific" numeric format string, with three decimal places. We've specified a custom numeric format string for "OrbitalPeriod".
We can set the horizontal alignment for all types of data (not just strings and dates). Numbers are a great candidate for non-left alignment however, as they become easier to read when the decimal points line up.
If we take the table from the format string example above, we can apply a central alignment to "DistanceFromSun" and a right alignment to "OrbitalPeriod":
table.Columns["DistanceFromSun"].Alignment = Alignment.Center;
table.Columns["OrbitalPeriod"].Alignment = Alignment.Right;
And we get the following:
╔═════════╦═════════════════╦═══════════════╗
║ Name ║ DistanceFromSun ║ OrbitalPeriod ║
╠═════════╬═════════════════╬═══════════════╣
║ Mercury ║ 5.791E+007 ║ 88.0 days ║
║ Venus ║ 1.082E+008 ║ 225.0 days ║
║ Earth ║ 1.496E+008 ║ 365.2 days ║
╚═════════╩═════════════════╩═══════════════╝