テーブルは、ドキュメント内の最も便利な機能の1つです。テーブルは、データを一覧表で表したり、ドキュメント内の他の要素をレイアウトするために使用できます。C1PrintDocument は、完全な機能を備えたテーブルを提供します。このセクションでは、テーブルの使用方法を説明します。ここでは、前のセクションで作成した「Hello, World!」サンプルアプリケーションを使用し、そのアプリケーションにテーブルを追加します。
Visual Basic コードの書き方
| Visual Basic |
コードのコピー
|
|---|---|
Dim rt As New RenderTable()
Me.C1PrintDocument1.Body.Children.Add(rt)
Dim row As Integer = 0
Do While (row < 10)
Dim col As Integer = 0
Do While (col < 6)
rt.Cells.Item(row, col).Text = String.Format("Cell ({0},{1})", row, col)
col += 1
Loop
row += 1
Loop
|
|
C# コードの書き方
| C# |
コードのコピー
|
|---|---|
RenderTable rt = new RenderTable();
this.c1PrintDocument1.Body.Children.Add(rt);
for (int row = 0; row < 10; ++ row)
{
for (int col = 0; col < 6; ++ col)
{
rt.Cells[row, col].Text = string.Format("Cell ({0},{1})", row, col);
}
}
|
|
Visual Basic コードの書き方
| Visual Basic |
コードのコピー
|
|---|---|
Me.C1PrintDocument1.Generate() |
|
C# コードの書き方
| C# |
コードのコピー
|
|---|---|
this.c1PrintDocument1.Generate(); |
|
アプリケーションをビルドし、実行します。プレビューには、次の画像に示すようなドキュメントが表示されます。

この単純なサンプルには、C1PrintDocument でテーブルを使用するための重要な点が含まれています。
Visual Basic コードの書き方
| Visual Basic |
コードのコピー
|
|---|---|
rt.Cells(10, 7).Text = "text at row 10, column 7" |
|
C# コードの書き方
| C# |
コードのコピー
|
|---|---|
rt.Cells[10, 7].Text = "text at row 10, column 7"; |
|
Visual Basic コードの書き方
| Visual Basic |
コードのコピー
|
|---|---|
rt.Style.GridLines.All = LineDef.Default |
|
C# コードの書き方
| C# |
コードのコピー
|
|---|---|
rt.Style.GridLines.All = LineDef.Default; |
|
デフォルトでは、テーブルは、その親のクライアント領域と同じ幅になり(このサンプルでは、ページ全体)、すべての列の幅が同じになります。ただし、行の高さは、自動的に設定されます。したがって、テーブル内の任意のセルのテキストを長いテキストに設定するコードを追加すると、そのセルを含む行は、すべてのテキストが入るように縦に広げられます。たとえば、次のコードをサンプルに追加します。
Visual Basic コードの書き方
| Visual Basic |
コードのコピー
|
|---|---|
rt.Cells(3, 4).Text = "A long line of text showing that table rows stretch " + "to accommodate all content." |
|
C# コードの書き方
| C# |
コードのコピー
|
|---|---|
rt.Cells[3, 4].Text = "A long line of text showing that table rows stretch " + "to accommodate all content."; |
|
次のようなテーブルが生成されます。このテーブルには、これまでに説明した2つの変更が含まれています。

参考までに、上のドキュメントを生成したロードイベントハンドラの完全なコードを次に示します。
Visual Basic コードの書き方
| Visual Basic |
コードのコピー
|
|---|---|
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.C1PrintDocument1.Body.Children.Add(New RenderText("Hello, World!"))
Dim rt As New RenderTable()
Me.C1PrintDocument1.Body.Children.Add(rt)
Dim row As Integer = 0
Do While (row < 10)
Dim col As Integer = 0
Do While (col < 6)
rt.Cells.Item(row, col).Text = String.Format("Cell ({0},{1})", row, col)
col += 1
Loop
row += 1
Loop
rt.Cells(3, 4).Text = "A long line of text showing that table rows " + "stretch to accommodate all content."
rt.Cells(10, 7).Text = "text at row 10, column 7"
rt.Style.GridLines.All = LineDef.Default
Me.C1PrintDocument1.Generate()
End Sub
|
|
C# コードの書き方
| C# |
コードのコピー
|
|---|---|
private void Form1_Load(object sender, EventArgs e)
{
this.c1PrintDocument1.Body.Children.Add(new RenderText("Hello, World!"));
RenderTable rt = new RenderTable();
this.c1PrintDocument1.Body.Children.Add(rt);
for (int row = 0; row < 10; ++row)
{
for (int col = 0; col < 6; ++col)
{
rt.Cells[row, col].Text = string.Format("Cell ({0},{1})", row, col);
}
}
rt.Cells[3, 4].Text = "A long line of text showing that table rows " + "stretch to accommodate all content.";
rt.Cells[10, 7].Text = "text at row 10, column 7";
rt.Style.GridLines.All = LineDef.Default;
this.c1PrintDocument1.Generate();
}
|
|