Table and Column Width, Row Height
Both rows and columns in C1PrintDocument tables can be auto-sized but the default behavior is different for rows and columns. By default, rows have auto-height (calculated based on the content of cells in the row), while columns' widths are fixed. The default width of a RenderTable is equal to the width of its parent, and that width is shared equally between all columns. So the following code will create a page-wide table, with 3 equally wide columns, and 10 rows with the heights that auto-adjust to the height of the cells' content:
Dim rt As New C1.C1Preview.RenderTable()
rt.Style.GridLines.All = LineDef.Default
Dim row As Integer = 0
Do While (row < 10)
Dim col As Integer = 0
Do While (col < 3)
rt.Cells(row, col).Text = String.Format( _
"Cell({0},{1})", row, col)
col += 1
Loop
row += 1
Loop
doc.Body.Children.Add(rt)
• C#
RenderTable rt = new RenderTable();
rt.Style.GridLines.All = LineDef.Default;
for (int row = 0; row < 10; ++row)
for (int col = 0; col < 3; ++col)
rt.Cells[row, col].Text = string.Format(
"Cell({0}, {1})", row, col);
doc.Body.Children.Add(rt);
To make a fully auto-sized table, as compared to the default settings, two things must be done:
• The width of the whole table must be set to Auto (either the string "auto", or the static field Unit.Auto)
and
• The table's RenderTable.ColumnSizingMode must be set to TableSizingModeEnum.Auto.
Here's the modified code:
Dim rt As New C1.C1Preview.RenderTable()
rt.Style.GridLines.All = LineDef.Default
Dim row As Integer = 0
Do While (row < 10)
Dim col As Integer = 0
Do While (col < 3)
rt.Cells(row, col).Text = String.Format( _
"Cell({0},{1})", row, col)
col += 1
Loop
row += 1
Loop
rt.Width = Unit.Auto
rt.ColumnSizingMode = TableSizingModeEnum.Auto
doc.Body.Children.Add(rt)
• C#
RenderTable rt = new RenderTable();
rt.Style.GridLines.All = LineDef.Default;
for (int row = 0; row < 10; ++row)
for (int col = 0; col < 3; ++col)
rt.Cells[row, col].Text = string.Format(
"Cell({0}, {1})", row, col);
rt.Width = Unit.Auto;
rt.ColumnSizingMode = TableSizingModeEnum.Auto;
doc.Body.Children.Add(rt);
The modified code makes each column of the table only as wide as needed to accommodate all text within cells.
|