$99 VS $1000+. UI Control for ASP.NET AJAX(contains chart and gridview).
GridView WinForms Control
Export grid data to excel, CSV, HTML, and PDF files in C# WinForms Applications.
Home > WinForms UI Controls > User Manual > Export Grid Data to HTML in C#

Export Data to HTML from GridView C#.NET

By using the Kettic GridView .NET WinForms control, the users can easily export the data in GridView to HTML and create an html format file to viewing in a web browser or MS Word. The HTML data exporting feature is included in the KetticData.dll assembly. To use this method, we should include the Kettic.WinForms.Data and the Kettic.WinForms.UI.Export C# namespaces, which allows us to access to the types contained in KetticData.

How to Export to HTML in C# GridView

To export the grid data to HTML, we need to initialize the ExportToHtml C# class first. And the constructor will take the parameter, KetticGridView, to be exported. The simple C# code below demonstrates the approach of initializing the ExportToHtml object.

ExportToHtml exporter = new ExportToHtml(this.ketticGridView1);
When we are going to export the grid data to html file, we can use this property to modify the default file extension, *.html, to the exported result file.

exporter.FileExtension = ".html";
After initializing the ExportToHtml C# class, we can use the following options to determine how to export data: ExportAlways, DoNotExport, and ExportAsHidden. The default setting is the DoNotExport, which will not export the hidden columns and rows to the html file. To accomplish, we need to modify the property of DoNotExport to HiddenColumnOption and HiddenRowOption. The C# code below shows how to set the hidden column option.

exporter.HiddenColumnOption = Kettic.WinForms.UI.Export.HiddenOption.DoNotExport;
The Kettic GridView allows the users to adjust the width of the column in grid. This is achieved through three options. The first option allows the users set fixed width to the specified columns, which will not be effected by the size of the grid columns in the actual Kettic GridView .NET control. The second one is to set fixed width for all columns, which will keep the aspect ratio. The third approach is to leave the column width being calculated automatically based on the content of the cells in the column.
The ExportToHTML C# class is able to export the visual settings to the HTML file. This feature is enabled via modifying the value of EnableAternativeRow property to true. The Kettic GridView only exports the conditional formatting to the HTML file and exports the row height at the default settings. To enable the functionality of exporting visual settings, set the value of ExportVisualSettings property to true as the C# code snippet below.

exporter.ExportVisualSettings = true;
There is a TableCaption property available to the users modifying the name of the table caption. The following is the C# code to set the table caption property

exporter.TableCaption = "Table";
The Kettic GridView .NET control offers the SummariesExportOption property to specify how to export summary items. There are four options available for selecting: ExportAll, ExportOnlyTop, ExportOnlyBottom, and DoNotExport. The default setting is ExportAll. The C# code below shows how to set the SummariesExportOption property.

exporter.SummariesExportOption = SummariesOption.DoNotExport;
After setting the above properties, we can export the data from grid to HTML now. To export data from grid to HTML, we need to use the ApplyExport method included in the ExportToHtml object. Its valid parameter includes fileName which define the name of the exported file. The following C# code shows how to run the ApplyExport method.

string fileName = "c:\\ExportDataToHTML.html";
exporter.RunExport(fileName);
The Kettic GridView C#.NET component allows the users to modify a single cell html element so that we can create formatting html cell for the exported GridView. This is accomplished by using the HtmlCellFormatting event. The C# code below shows how to handle this event.
Moreover, the HtmlTableCaptionFormatting event allows the users to create formatting caption table element on the html. This is accomplished by accessing the TableCaptionElement via event arguments and applying a valid html format. The C# code below demonstrates how to handle the HtmlTableCaptionFormat event.
UI Controlsfor Windows Forms
.NET WinForms UI Overview.NET WinForms UI Features.NET WinForms UI GuideC# WinForms UI DesignVB.NET WinForms UI Design
WinForms UI Controls