ComponentOne True DBGrid for WinForms
Tutorial 15: Using PrintInfo and Print Preview
True DBGrid for WinForms Tutorials > Tutorial 15: Using PrintInfo and Print Preview

In this tutorial, you will learn how to use the printing and exporting capabilities of True DBGrid for WinForms.

Complete the following steps:

  1. Start with the project created in Tutorial 1: Binding True DBGrid to a DataSet.
  2. Add one Button to the form (Button1) and change its Text property to "Print Preview".
  3. Enter the following code in the Load event of Form1. It changes the BackColor of a column, changes a column's font, sets the NumberFormat property for a column to the FormatText event, and changes the HeadingStyle:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' Change the presentation of the grid.
    With Me.C1TrueDBGrid1.Splits(0).DisplayColumns
        .Item("Country").Style.BackColor = System.Drawing.Color.Cyan
        Dim fntFont As Font
        fntFont = New Font("Times New Roman", .Item("Country").Style.Font.Size, FontStyle.Regular)
        .Item("Country").Style.Font = fntFont
        .Item("Last").Style.ForeColor = System.Drawing.Color.Red
    End With
    Me.C1TrueDBGrid1.Columns("last").NumberFormat = "FormatText Event"
    With Me.C1TrueDBGrid1.HeadingStyle
        Dim fntfont As Font
        fntfont = New Font(.Font.Name, .Font.Size, FontStyle.Bold)
        .Font = fntfont
        .BackColor = System.Drawing.Color.Blue
        .ForeColor = System.Drawing.Color.Yellow
    End With
    

    To write code in C#

    C#
    Copy Code
    // Change the presentation of the grid.
    C1DisplayColumn col = this.c1TrueDBGrid1.Splits[0].DisplayColumns["Country"];
    col.Style.BackColor = System.Drawing.Color.Cyan;
    Font fntFont;
    fntFont = new Font("Times new Roman", col.Style.Font.Size, FontStyle.Regular);
    col.Style.Font = fntFont;
    c1TrueDBGrid1.Splits[0].DisplayColumns["Last"].Style.ForeColor = System.Drawing.Color.Red;
    this.c1TrueDBGrid1.Columns["last"].NumberFormat = "FormatText event";
    Font fntfont;
    fntfont = new Font(Font.Name, this.c1TrueDBGrid1.HeadingStyle.Font.Size, FontStyle.Bold);
    this.c1TrueDBGrid1.HeadingStyle.Font = fntfont;
    this.c1TrueDBGrid1.HeadingStyle.BackColor = System.Drawing.Color.Blue;
    this.c1TrueDBGrid1.HeadingStyle.ForeColor = System.Drawing.Color.Yellow;
    
  4. In the previous code the NumberFormat property for a column was set to FormatText. This means that the FormatText event will fire enabling the programmer to change the style and format of column values. Enter the following code into the FormatText event, which changes the column values to uppercase:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Private Sub C1TrueDBGrid1_FormatText(ByVal sender As Object, ByVal e As C1.Win.C1TrueDBGrid.FormatTextEventArgs) Handles C1TrueDBGrid1.FormatText
        e.Value = UCase(e.Value)
    End Sub
    

    To write code in C#

    C#
    Copy Code
    private void C1TrueDBGrid1_FormatText(object sender,  C1.Win.C1TrueDBGrid.FormatTextEventArgs e) 
    {
        e.Value = e.Value.ToUpper();
    }
    
  5. Add the following code to the Click event of Button1. It uses the PrintInfo object and its properties and methods to create a print page header and footer. It ends by calling the PrintPreview method that invokes the Print Preview window:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    With Me.C1TrueDBGrid1.PrintInfo
        Dim fntFont As Font
        fntFont = New Font(.PageHeaderStyle.Font.Name, .PageHeaderStyle.Font.Size, FontStyle.Italic)
        .PageHeaderStyle.Font = fntFont
        .PageHeader = "Composers Table"
     
        ' Column headers will be on every page.
        .RepeatColumnHeaders = True
     
        ' Display page numbers (centered).
        .PageFooter = "Page: \p"
     
        ' Invoke print preview.
        .UseGridColors = True
        .PrintPreview()
    End With
    

    To write code in C#

    C#
    Copy Code
    Font fntFont;
    fntFont = new Font(this.c1TrueDBGrid1.PrintInfo.PageHeaderStyle.Font.Name, this.c1TrueDBGrid1.PrintInfo.PageHeaderStyle.Font.Size, FontStyle.Italic);
    this.c1TrueDBGrid1.PrintInfo.PageHeaderStyle.Font = fntFont;
    this.c1TrueDBGrid1.PrintInfo.PageHeader = "Composers Table";
     
    // Column headers will be on every page.
    this.c1TrueDBGrid1.PrintInfo.RepeatColumnHeaders = true;
     
    // Display page numbers (centered).
    this.c1TrueDBGrid1.PrintInfo.PageFooter = "Page: \\p";
     
    // Invoke print preview.
    this.c1TrueDBGrid1.PrintInfo.UseGridColors = true;
    this.c1TrueDBGrid1.PrintInfo.PrintPreview();
    

Run the program and observe the following:

You've successfully completed using PrintInfo and Print Preview; this concludes tutorial 15.