ComponentOne True DBGrid for .NET (2.0) Search HelpCentral 

Tutorial 15 - PrintInfo and Print Preview

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

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:

·      Visual Basic

' 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

·      C#

// 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;

·      Delphi

procedure TWinForm.TWinForm_Load(sender: System.Object; e: System.EventArgs);

var

  Style: C1.Win.C1TrueDBGrid.Style;

  FntFont: System.Drawing.Font;

  Split: C1.Win.C1TrueDBGrid.Split;

begin

  Split := Self.C1TrueDBGrid1.Splits[0];

  Split.DisplayColumns['Country'].Style.BackColor := Color.Cyan;

  FntFont := System.Drawing.Font.Create('Times New Roman', Split.DisplayColumns['Country'].Style.Font.Size, FontStyle.Regular);

  Split.DisplayColumns['Country'].Style.Font := fntFont;

  Split.DisplayColumns['Last'].Style.ForeColor := System.Drawing.Color.Red;

  Self.C1TrueDBGrid1.Columns['Last'].NumberFormat := 'FormatTextEvent';

  Style := C1TrueDBGrid1.HeadingStyle;

  FntFont := System.Drawing.Font.Create(style.Font.Name, style.Font.Size, FontStyle.Bold);

  Style.BackColor := System.Drawing.Color.Blue;

  Style.ForeColor := System.Drawing.Color.Yellow;

end;

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:

·      Visual Basic

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

·      C#

private void C1TrueDBGrid1_FormatText( object sender,  C1.Win.C1TrueDBGrid.FormatTextEventArgs e)

{

    e.value = e.Value.ToUpper();

}

·      Delphi

procedure TWinForm.C1TrueDBGrid1_FormatText(sender: System.Object; e: C1.Win.C1TrueDBGrid.FormatTextEventArgs);

begin

  e.Value := e.Value.ToUpper;

end;

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:

·      Visual Basic

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

·      C#

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

·      Delphi

procedure TWinForm.Button1_Click(sender: System.Object; e: System.EventArgs);

var

  FntFont: System.Drawing.Font;

  PrintInfo: C1.Win.C1TrueDBGrid.PrintInfo;

begin

  PrintInfo := C1TrueDBGrid1.PrintInfo;

  FntFont := System.Drawing.Font.Create(printInfo.PageHeaderStyle.Font.Name, printInfo.PageHeaderStyle.Font.Size, FontStyle.Italic);

  PrintInfo.PageHeaderStyle.Font := fntFont;

  PrintInfo.PageHeader := 'Composer Table';

  PrintInfo.RepeatColumnHeaders := True;

  PrintInfo.PageFooter := 'Page: \p';

  PrintInfo.UseGridColors := True;

  PrintInfo.PrintPreview;

end;

Run the program and observe the following:

·      C1TrueDBGrid1 displays the data using the font and color changes specified in step 4.

·      Click the Print Preview button to display a separate application window. Note that the output mirrors the format of the grid.

This concludes the tutorial.


Send comments about this topic to ComponentOne.
Copyright © ComponentOne LLC. All rights reserved.