Reports for WinForms
単純なテーブルを作成する
クイックスタート > 印刷プレビューを開始する > テーブルを作成する > 単純なテーブルを作成する

テーブルは、ドキュメント内の最も便利な機能の1つです。テーブルは、データを一覧表で表したり、ドキュメント内の他の要素をレイアウトするために使用できます。C1PrintDocument は、完全な機能を備えたテーブルを提供します。このセクションでは、テーブルの使用方法を説明します。ここでは、前のセクションで作成した「Hello, World!」サンプルアプリケーションを使用し、そのアプリケーションにテーブルを追加します。

メモ:このトピック内のサンプルコードは、「using C1.C1Preview」ディレクティブ(C# の場合。他の言語の場合は相当する構文)がファイルに挿入されていることを前提としています。したがって、完全修飾された型名(C1.C1Preview.RenderText など)ではなく、クラス名部分(RenderText)だけを使用します。
  1. 前のセクションで作成した HelloWorld アプリケーションを開きます。または、新しい Windows Forms アプリケーションを作成し、フォームに C1PrintPreviewControl とC1PrintDocumentを追加して、前のセクションで説明したようにドキュメントをプレビューコントロールにアタッチすることもできます。
  2. Form Load イベントハンドラ(必要に応じて作成)で、c1PrintDocument1.Generate () の呼び出しの前に、次のコードを追加します。

    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);   
        }   
    }
    
  3. ドキュメントの Generate メソッドを忘れずに呼び出します。

    Visual Basic コードの書き方

    Visual Basic
    コードのコピー
    Me.C1PrintDocument1.Generate()
    

    C# コードの書き方

    C#
    コードのコピー
    this.c1PrintDocument1.Generate();
    

アプリケーションをビルドし、実行します。プレビューには、次の画像に示すようなドキュメントが表示されます。

この単純なサンプルには、C1PrintDocument でテーブルを使用するための重要な点が含まれています。

参考までに、上のドキュメントを生成したロードイベントハンドラの完全なコードを次に示します。

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();    
}