In this article I will explain with an example, how to send DataGridView to Email in Windows Forms (WinForms) Application using C# and VB.Net.
First a DataGridView will be populated with some data and then an HTML Table will be generated using the values from the DataGridView.
Finally, the HTML Table string will be used to send the DataGridView’s data to Email in Windows Forms (WinForms) Application using C# and VB.Net.
 
 
Form Design
The Form consists of a DataGridView and a Button.
Send DataGridView to Email in Windows Application using C# and VB.Net
 
 
Namespaces
You will need to import the following namespaces.

C#
using System.Data;
using System.Net;
using System.Net.Mail;
 
VB.Net
Imports System.Data
Imports System.Net
Imports System.Net.Mail
 
 
Populating the DataGridView
Inside the Form Load event handler, the DataGridView is populated with data by making use of a dynamic DataTable with some records.

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");
    this.dataGridView1.DataSource = dt;
    this.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")
    Me.dataGridView1.DataSource = dt
    Me.dataGridView1.AllowUserToAddRows = False
End Sub
 
 
Send DataGridView to Email in Windows Application
When the Send 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 used to send the DataGridView’s data to Email in Windows (WinForms) Application using C# and VB.Net.
Note: For more details on sending email in Windows Forms (WinForms) Application, please refer my article Send email with Attachment using Windows Application (Windows Forms) in C# and VB.Net.
 

C#
private void btnSend_Click(object sender, EventArgs e)
{
    //Table start.
    string html = "<table cellpadding='5' cellspacing='0' style='border: 1px solid #ccc;font-size: 9pt'>";
 
    //Adding HeaderRow.
    html += "<tr>";
    foreach (DataGridViewColumn column in dataGridView1.Columns)
    {
        html += "<th style='background-color: #B8DBFD;border: 1px solid #ccc'>" + column.HeaderText + "</th>";
    }
    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() + "</td>";
        }
        html += "</tr>";
    }
 
    //Table end.
    html += "</table>";
 
 
    //Sending the DataGridView's HTML in Email.
    using (MailMessage mm = new MailMessage("sender@gmail.com", "recipient@gmail.com"))
    {
        mm.Subject = "DataGridView";
        mm.Body = html;
        mm.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        NetworkCredential NetworkCred = new NetworkCredential(mm.From.Address, "xxxxxx");
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;
        smtp.Send(mm);
        MessageBox.Show("Email sent.", "Message");
    }
}
 
VB.Net
Private Sub btnSend_Click(sender As System.Object, e As System.EventArgs) Handles btnSend.Click
    'Table start.
    Dim html As String = "<table cellpadding='5' cellspacing='0' style='border: 1px solid #ccc;font-size: 9pt'>"
 
    '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>"
 
    'Sending the DataGridView's HTML in Email.
    Using mm As New MailMessage("sender@gmail.com", "recipient@gmail.com")
        mm.Subject = "DataGridView"
        mm.Body = html
        mm.IsBodyHtml = True
        Dim smtp As New SmtpClient()
        smtp.Host = "smtp.gmail.com"
        smtp.EnableSsl = True
        Dim NetworkCred As New NetworkCredential(mm.From.Address, "xxxxxx")
        smtp.UseDefaultCredentials = True
        smtp.Credentials = NetworkCred
        smtp.Port = 587
        smtp.Send(mm)
        MessageBox.Show("Email sent.", "Message")
    End Using
End Sub
 
 
Screenshot
Send DataGridView to Email in Windows Application using C# and VB.Net
 
 
Downloads