Finally, the
HTML Table string will be saved to a Folder (Directory) in
Windows Forms (WinForms) Application using C# and VB.Net.
Form Design
Namespaces
You will need to import the following namespace.
C#
VB.Net
Populating the DataGridView
Inside the
Form Load event handler, an object of
DataTable is created.
Then, three columns are added to the
DataTable Columns collection using the
AddRange method.
Inside the AddRange method, an Array of the objects of type DataColumn is specified to which, the name and the optional parameter Data Type i.e. the Type of the column is passed.
Once the schema is ready i.e. all the columns are defined and some rows have been added using the Rows.Add method.
C#
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country", typeof(string)) });
dt.Rows.Add(1,"John Hammond", "United States");
dt.Rows.Add(2,"Mudassar Khan", "India");
dt.Rows.Add(3,"Suzanne Mathews", "France");
dt.Rows.Add(4,"Robert Schidner", "Russia");
dataGridView1.DataSource = dt;
dataGridView1.AllowUserToAddRows false;
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn() {
New DataColumn("Id", GetType(Integer)),
New DataColumn("Name", GetType(String)),
New DataColumn("Country", GetType(String))})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
dataGridView1.DataSource = dt
dataGridView1.AllowUserToAddRows = False
End Sub
Export DataGridView to HTML in Windows Application
When the
Export Button is clicked, an
HTML Table is created with string concatenation.
The
HTML Table will contain columns same as that of the
DataGridView and then a loop is executed over the
DataGridView columns to add their header texts to the Header row of the
HTML Table.
Once the Header row is populated then loop is executed over the
DataGridView rows to add data rows to the
HTML Table.
Finally, the
HTML Table string will be saved to a Folder (Directory) in
Windows Forms (WinForms) Application using C# and VB.Net.
C#
private void btnExport_Click(object sender, EventArgs e)
{
//Table start.
string html = "<table cellpadding='5' cellspacing='0' style='border: 1px solid #ccc;font-size: 9pt; font-family:arial'>";
//Adding HeaderRow.
html += "<tr>";
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
html += "<th style='background-color: #B8DBFD;border: 1px solid #ccc'>" + column.HeaderText + "";
}
html += "</tr>";
//Adding DataRow.
foreach (DataGridViewRow row in dataGridView1.Rows)
{
html += "<tr>";
foreach (DataGridViewCell cell in row.Cells)
{
html += "<td style='width:120px;border: 1px solid #ccc'>" + cell.Value.ToString() + "";
}
html += "</tr>";
}
//Table end.
html += "</table>";
File.WriteAllText(@"E:\Files\DataGridView.htm", html);
}
VB.Net
Private Sub btnExport_Click(sender As System.Object, e As System.EventArgs) Handles btnExport.Click
'Table start.
Dim html As String = "<table cellpadding='5' cellspacing='0' style='border: 1px solid #ccc; font-size: 9pt; font-family:arial'>"
'Adding HeaderRow.
html &= "<tr>"
For Each column As DataGridViewColumn In dataGridView1.Columns
html &= "<th style='background-color: #B8DBFD; border: 1px solid #ccc'>" & column.HeaderText & "</th>"
Next
html &= "</tr>"
'Adding DataRow.
For Each row As DataGridViewRow In dataGridView1.Rows
html &= "<tr>"
For Each cell As DataGridViewCell In row.Cells
html &= "<td style='width:120px;border: 1px solid #ccc'>" & cell.Value.ToString() & "</td>"
Next
html &= "</tr>"
Next
'Table end.
html &= "</table>"
File.WriteAllText("E:\Files\DataGridView.htm", html)
End Sub
Screenshots
The DataGridView
The exported HTML File
Downloads