ComponentOne True DBGrid for WinForms
True DBGrid for WinForms Task-Based Help / Creating a Custom Print Preview
In This Topic
    Creating a Custom Print Preview
    In This Topic

    You can create a custom print preview and customize how your grid will appear when printed. You can do this using the Init method. To override properties like FormBorderStyle, MaximizeBox, MinimizeBox, ControlBox and so on of a Form inherited from C1.Win.C1TrueDBGrid.PrintForm, override the Init method of the PrintForm. First call the base.Init(), then set the properties you want.

    Complete the following steps:

    1. Navigate to the Toolbox and double-click the SplitContainer panel to add it to the Form.
    2. Navigate to the Properties window and set the SplitContainer panel's Orientation property to Horizontal.
    3. Click in the top panel of the SplitContainer, navigate to the Toolbox and double-click the Button control to add it to the application.
    4. In the Properties window, set the Button control's Text property to "Preview".
    5. Click in the bottom panel of the C1SplitContainer, navigate to the Toolbox, and locate and then double-click the C1TrueDBGrid control to add it to the application.
    6. Click the C1TrueDBGrid control's smart tag and choose the Dock in Parent Container option from the Tasks menu.
    7. Right-click the project in the Solution Explorer and select Add Reference. In the Add Reference dialog box, locate and select the C1.Win.RibbonPreview.4.5.2 and C1.Win.Ribbon.4.5.2 assemblies and click OK. This is required for enabling print preview.
      Note: TrueDBGrid's export and printing features uses the C1.Win.Printing NuGet package, which provides three libraries: C1.PrintDocument, C1.Win.PrintPreview and C1.Win.RibbonPreview. Each library provides a set of previewing controls or components. The PrintDocument library provides the PrintDocument and MultiDocument components, the PrintPreview library provides the PreviewOutlineView, PreviewPane, PreviewTextSearchPanel, Thumbnail, PrintPreviewControl and PrintPreviewDialog components, and RibbonPreview  provides the RibbonPreview and RibbonPreviewDialog components. Since C1Report is now obsolete, make sure that the references for C1Report libraries is replaced by  individual library references in the C1.Win.Printing nuget.
    8. Double-click the Form to switch to Code view and create the Form_Load event handler.
    9. Add the following code to the Form_Load event:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      FillGrid()
      

      To write code in C#

      C#
      Copy Code
      FillGrid();
      
    10. Add the FillGrid event just below the Form_Load event:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Private Sub FillGrid()
          Dim maxrows As Integer = 5
       
          Dim dt As New DataTable("testdatatable")
       
          Dim dc As DataColumn
          Dim dr As DataRow
       
          ' set up an integer column
          dc = New DataColumn()
          dc.DataType = System.Type.[GetType]("System.DateTime")
          dc.ColumnName = "DT1"
          dt.Columns.Add(dc)
       
          ' do string
          dc = New DataColumn()
          dc.DataType = System.Type.[GetType]("System.DateTime")
          dc.ColumnName = "DT2"
          dt.Columns.Add(dc)
       
          ' do string
          dc = New DataColumn()
          dc.DataType = System.Type.[GetType]("System.DateTime")
          dc.ColumnName = "DT3"
          dt.Columns.Add(dc)
       
          Dim rnd As New Random()
          For i As Integer = 0 To maxrows - 1
              dr = dt.NewRow()
              dr("DT1") = DateTime.Now.AddDays(i)
              dr("DT2") = DateTime.Now.AddMonths(i)
              dr("DT3") = DateTime.Now.AddYears(i)
              dt.Rows.Add(dr)
          Next
          Me.C1TrueDBGrid1.DataSource = dt
          Me.C1TrueDBGrid1.Columns("DT1").EnableDateTimeEditor = True
          Me.C1TrueDBGrid1.Columns("DT2").EnableDateTimeEditor = True
          Me.C1TrueDBGrid1.Columns("DT3").EnableDateTimeEditor = True
      End Sub
      

      To write code in C#

      C#
      Copy Code
      private void FillGrid()
      {
          int maxrows = 5;
       
          DataTable dt = new DataTable("testdatatable");
       
          DataColumn dc;
          DataRow dr;
       
          // set up an integer column
          dc = new DataColumn();
          dc.DataType = System.Type.GetType("System.DateTime");
          dc.ColumnName = "DT1";
          dt.Columns.Add(dc);
       
          // do string
          dc = new DataColumn();
          dc.DataType = System.Type.GetType("System.DateTime");
          dc.ColumnName = "DT2";
          dt.Columns.Add(dc);
       
          // do string
          dc = new DataColumn();
          dc.DataType = System.Type.GetType("System.DateTime");
          dc.ColumnName = "DT3";
          dt.Columns.Add(dc);
       
          Random rnd = new Random();
          for (int i = 0; i < maxrows; i++)
          {
              dr = dt.NewRow();
              dr["DT1"] = DateTime.Now.AddDays(i); ;
              dr["DT2"] = DateTime.Now.AddMonths(i);
              dr["DT3"] = DateTime.Now.AddYears(i);
              dt.Rows.Add(dr);
          }
          this.c1TrueDBGrid1.DataSource = dt;
          this.c1TrueDBGrid1.Columns["DT1"].EnableDateTimeEditor = true;
          this.c1TrueDBGrid1.Columns["DT2"].EnableDateTimeEditor = true;
          this.c1TrueDBGrid1.Columns["DT3"].EnableDateTimeEditor = true;
      }
      
    11. In the Solution Explorer, right-click the project and select Add | Windows Form. In the Add New Item dialog box, name the form "PrintForm1" and click the Add button.
    12. Double-click the new form to switch to Code view.
    13. Edit the initial class declaration to inherit from C1.Win.C1TrueDBGrid.PrintForm:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Public Class PrintForm1
          Inherits C1.Win.C1TrueDBGrid.PrintForm
      

      To write code in C#

      C#
      Copy Code
      public partial class PrintForm1 : C1.Win.C1TrueDBGrid.PrintForm
      
    14. Add the following code below the class declaration:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Protected Overrides Sub Init()
          MyBase.Init()
          FormBorderStyle = FormBorderStyle.Sizable
          Me.ControlBox = True
          Me.MinimizeBox = False
          Me.MaximizeBox = False
      End Sub
      

      To write code in C#

      C#
      Copy Code
      protected override void Init()
      {
          base.Init();
          FormBorderStyle = FormBorderStyle.Sizable;
          this.ControlBox = true;
          this.MinimizeBox = false;
          this.MaximizeBox = false;
      }
      
    15. Return to Form1 in Design view and double-click the Button to switch to Code view and create the Button_Click event handler.
    16. Add the following code to the Button_Click event handler, making sure to replace "ProjectName" with the name of your project:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      C1TrueDBGrid1.PrintInfo.PreviewFormClassName = "ProjectName.PrintForm"
      C1TrueDBGrid1.PrintInfo.PrintPreview()
      

      To write code in C#

      C#
      Copy Code
      c1TrueDBGrid1.PrintInfo.PreviewFormClassName = "ProjectName.PrintForm1";
      c1TrueDBGrid1.PrintInfo.PrintPreview();
      

    What You've Accomplished

    Run the application  and notice the application appears with a button and grid displaying data. Click the Preview button and observe that a customized print preview form appears. The form only includes the Close button and not the Minimize and Maximize buttons.