True DBGrid for WinForms | ComponentOne
In This Topic
    Export
    In This Topic

    The data in the grid can be exported as a delimited text, Excel, HTML, PDF, or RTF file. The following table describes the methods used to export each file type:

    File Type Method Description
    All ExportTo Opens a dialog box in which the user can select the export format.
    Delimited Text ExportToDelimitedFile Exports the specified rows from the grid to the specified file as delimited text.
    Excel ExportToExcel Exports the grid to an Excel file.
    HTML ExportToHTML Exports the grid to an HTML file.
    PDF ExportToPDF Exports the grid to a PDF file.
    RTF ExportToRTF Exports the grid to an RTF file.
    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.

    Export to Available File Types

    Users can export to available file types by setting the ExportTo method.

    C#
    Copy Code
    this.c1TrueDBGrid1.ExportTo();
    

    This topic illustrates the following:

    Clicking the Export button opens the TrueDBGrid Print/Export Options dialog box.

    1. In the Action drop-down list, select the file type, including metafiles and image files.
    2. Click the ellipsis button next to the File name box to open the Export To dialog box. Browse to a location to save the file and enter the file name in the File name box. Click OK to close the Export To dialog box.
    3. Under Page Headers and Footers, add Header text and Footer text.
    4. Click OK to export the file.

    Export to Delimited Text

    To set the ExportToDelimitedFile method, add the following code to the Click event of the Export button:

    C#
    Copy Code
    private void button1_Click(object sender, EventArgs e)
            {
                c1TrueDBGrid1.ExportToDelimitedFile(@"c:\temp\composers.csv", C1.Win.C1TrueDBGrid.RowSelectorEnum.AllRows, ",");
            }
    

    Clicking the Export button creates a delimited text file in the temp directory specified in the code above. Each value in the file is separated by a comma:

    Export to Excel

    TrueDBGrid allows exporting grid data to Microsoft Excel format by using either of the following methods:

    It is important to note that time taken to export grid data to Excel format is very small when using the SaveExcel method. In comparison, the ExportToExcel method takes much longer to export data.

    Please note that a reference to the C1.Win.C1TrueDBGrid.Excel library is required in order to use SaveExcel extension method of the C1TrueDBGrid in your project.

    To set the SaveExcel method, add the following code to the Click event of the Export button:

    C#
    Copy Code
    c1TrueDBGrid1.SaveExcel("../../GridData.xlsx");
    

    Clicking the Export button creates an Excel file as given below:

    C#
    Copy Code
    this.c1TrueDBGrid1.SaveExcel("../../GridData.xlsx");
       

    To set the ExportToExcel method, add the following code to the Click event of the Export button:

    C#
    Copy Code
    c1TrueDBGrid1.ExportToExcel(@"c:\temp\composers.xls");
    
    C#
    Copy Code
    this.c1TrueDBGrid1.ExportToExcel(@"c:\temp\composers.xls");
    

    Clicking the Export button creates an Excel file in the temp directory indicated in the code above:

    Export to HTML

    To set the ExportToHTML method, add the following code to the Click event of the Export button:

    C#
    Copy Code
    c1TrueDBGrid1.ExportToHTML(@"c:\temp\composers.html");
    

    Clicking the Export button creates an HTML file in the temp directory indicated in the code above:

    Export to PDF

    To set the ExportToPDF method, add the following code to the Click event of the Export button:

    C#
    Copy Code
    c1TrueDBGrid1.ExportToPDF(@"c:\temp\composers.pdf");
    

    Clicking the Export button creates a PDF file in the temp directory:

    Export to RTF

    To set the ExportToRTF method, add the following code to the Click event of the Export button:

    C#
    Copy Code
    c1TrueDBGrid1.ExportToRTF(@"c:\temp\composers.rtf");
    

    Clicking the Export button creates a RTF file in the temp directory indicated in the code above: