-
Set up your form.
-
In a new Visual Basic or C# project, place an UltraGrid and an UltraButton control on the form.
-
Double-click the UltraGridDocumentExporter component in the toolbox. The component is added to the component tray.
-
With the button selected, find the Text property in the Properties window and change it to "Export to PDF".
-
Create the BeginExport event.
Create the BeginExport event for the ultraGridDocumentExporter1 component either through the Properties window if using C# or the drop-down boxes above the code-behind if using Visual Basic. In this event, you will be performing two actions: setting the margins of the document and hiding several columns.
Private Sub UltraGridDocumentExporter1_BeginExport(ByVal sender As Object, _
ByVal e As Infragistics.Win.UltraWinGrid.DocumentExport.BeginExportEventArgs) _
Handles UltraGridDocumentExporter1.BeginExport
' This line effectively gives the page a 25 pixel margin.
e.Section.PageMargins.All = 25
' Hides columns 1, 3, 4, 7, and 11 (they will not be exported).
e.Layout.Bands(0).Columns(0).Hidden = True
e.Layout.Bands(0).Columns(2).Hidden = True
e.Layout.Bands(0).Columns(3).Hidden = True
e.Layout.Bands(0).Columns(6).Hidden = True
e.Layout.Bands(0).Columns(10).Hidden = True
End Sub
private void ultraGridDocumentExporter1_BeginExport(object sender,
Infragistics.Win.UltraWinGrid.DocumentExport.BeginExportEventArgs e)
{
// This line effectively gives the page a 25 pixel margin.
e.Section.PageMargins.All = 25;
// Hides columns 1, 3, 4, 7, and 11 (they will not be exported).
e.Layout.Bands[0].Columns[0].Hidden = true;
e.Layout.Bands[0].Columns[2].Hidden = true;
e.Layout.Bands[0].Columns[3].Hidden = true;
e.Layout.Bands[0].Columns[6].Hidden = true;
e.Layout.Bands[0].Columns[10].Hidden = true;
}
-
Create the InitializeRow event.
Create the InitializeRow event for the ultraGridDocumentExporter1 component either through the Properties window if using C# or the drop-down boxes above the code-behind if using Visual Basic. The code in this event will only export rows whose company name starts with the letter "F" as well as change the foreground color of the tenth cell to Red.
Private Sub UltraGridDocumentExporter1_InitializeRow(ByVal sender As Object, _
ByVal e As Infragistics.Win.UltraWinGrid.DocumentExport.DocumentExportInitializeRowEventArgs) _
Handles UltraGridDocumentExporter1.InitializeRow
' If the text in the cell does NOT start with 'F', skip the current row.
If (Not e.Row.Cells(1).Text.StartsWith("F")) Then
e.SkipRow = True
End If
' Change the text color of the tenth cell to red.
e.Row.Cells(9).Appearance.ForeColor = Color.Red
End Sub
private void ultraGridDocumentExporter1_InitializeRow(object sender,
Infragistics.Win.UltraWinGrid.DocumentExport.DocumentExportInitializeRowEventArgs e)
{
// If the text in the cell does NOT start with 'F', skip the current row.
if (!e.Row.Cells[1].Text.StartsWith("F")) e.SkipRow = true;
e.Row.Cells[9].Appearance.ForeColor = Color.Red;
}
-
Create the CellExporting event.
Create the CellExporting event for the ultraGridDocumentExporter1 component either through the Properties window if using C# or the drop-down boxes above the code-behind if using Visual Basic. The code in the CellExporting event will change the Value of the tenth cell in each row to "***Confidential*** ".
Private Sub UltraGridDocumentExporter1_CellExporting(ByVal sender As Object, _
ByVal e As Infragistics.Win.UltraWinGrid.DocumentExport.CellExportingEventArgs) _
Handles UltraGridDocumentExporter1.CellExporting
' Change the value of the tenth cell of each row.
If (e.GridColumn.Index = 9) Then
e.ExportValue = "$$*$$$$*$$$$*$$Confidential$$*$$$$*$$$$* $$"
End If
End Sub
private void ultraGridDocumentExporter1_CellExporting(object sender,
Infragistics.Win.UltraWinGrid.DocumentExport.CellExportingEventArgs e)
{
// Change the value of the tenth cell.
if (e.GridColumn.Index == 9) e.ExportValue = "$$*$$$$*$$$$*$$Confidential$$*$$$$*$$$$* $$";
}
-
Export the grid through the button’s Click event.
In Design view, double-click the button to create the Click event. In this event, you will call the Export method off the UltraGridDocumentExporter. The fourth overload accepts three parameters:
-
the WinGrid that you want to export (UltraGrid)
-
the file name and path (System.String)
-
the file format you want to export the grid as (GridExportFileFormat)
You will export WinGrid as a PDF file to the root of C: and name it GridPDF.pdf.
Private Sub UltraButton1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles UltraButton1.Click
Me.UltraGridDocumentExporter1.Export( _
Me.UltraGrid1, _
"C:\GridPDF.pdf", _
Infragistics.Win.UltraWinGrid.DocumentExport.GridExportFileFormat.PDF)
End Sub
private void ultraButton1_Click(object sender, EventArgs e)
{
this.ultraGridDocumentExporter1.Export(
this.ultraGrid1,
@"C:\GridPDF.pdf",
Infragistics.Win.UltraWinGrid.DocumentExport.GridExportFileFormat.PDF);
}
When you run the application, you will see WinGrid fully populated with the entire Customers table from the Northwind database. When you click the button, a PDF file is created and placed in the root of C:. When you view the PDF file, it should look something like the following screen shot.
Note
|
Note
If you’d like quicker access to your new PDF file, you can call the System.Diagnostic.Process.Start() method in the button’s Click event and pass in the full filename and path as the parameter.
|