Category / Section
How to print the contents of the Grid to fit the width of the page in WinForms GridControl?
2 mins read
Printing
To print the contents of the grid to fit into the width of the page, it can be done by using GridPrintDocument without any page breaks. GridPrintToFitDocument can be used to print the document properly which is derived from the GridPrintDocument. To utilize this class, you need to add the GridHelperClasses.Windows.dll as reference.
C#
private void button3_Click(object sender, EventArgs e) { if (this.gridControl1 != null) { try { //uses the default printer GridPrintToFitDocument pd = new GridPrintToFitDocument(this.gridControl1, true); PrintPreviewDialog dlg = new PrintPreviewDialog(); dlg.Document = pd; pd.DefaultPageSettings.Landscape = true; dlg.ShowDialog(); } catch (Exception ex) { MessageBox.Show("An error occurred attempting to preview the grid - " + ex.Message); } } if (this.gridControl1 != null) { try { GridPrintToFitDocument pd = new GridPrintToFitDocument(this.gridControl1, true); PrintDialog dlg = new PrintDialog(); dlg.Document = pd; pd.DefaultPageSettings.Landscape = true; if (dlg.ShowDialog() == DialogResult.OK) pd.Print(); } catch (Exception ex) { MessageBox.Show("An error occurred attempting to print the grid - " + ex.Message); } } }
VB
Private Sub button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button3.Click If Me.gridControl1 IsNot Nothing Then Try 'uses the default printer Dim pd As New GridPrintToFitDocument(Me.gridControl1, True) Dim dlg As New PrintPreviewDialog() dlg.Document = pd pd.DefaultPageSettings.Landscape = True dlg.ShowDialog() Catch ex As Exception MessageBox.Show("An error occurred attempting to preview the grid - " & ex.Message) End Try End If If Me.gridControl1 IsNot Nothing Then Try Dim pd As New GridPrintToFitDocument(Me.gridControl1, True) Dim dlg As New PrintDialog() dlg.Document = pd pd.DefaultPageSettings.Landscape = True If dlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then pd.Print() End If Catch ex As Exception MessageBox.Show("An error occurred attempting to print the grid - " & ex.Message) End Try End If End Sub
Samples:
C#: Print page
VB: Print page
Reference link: https://help.syncfusion.com/windowsforms/grid-control/printing