Good Day Vb.net geniuses! I would like to ask what is the easiest way to email the data's inside the datagridview? or if possible the whole table :) I searched some tutorials in the web but all of them are quite complicated. Any idea guys?

Probably the easiest is to, use a text file. Have each row on one line of a file with the values, converted to string, comma delimited. Use a .csv extension and the receiver should be able to load it into a spreadsheet. For readability maybe make the first line with column headings. A For Each loop to iterate thourgh the rows, and code to set up the strings for each value, then add a newline to finish the string for that row.

Edited 3 Years Ago by tinstaafl

@Ancient Dragon - I started learning vb.net with the link you provided and unfortunately there is no topic there regarding emails for datagridview :( I used this basic code to email messages :)

Imports System.Net.Mail

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim email As New MailMessage()
        Try
            email.From = New MailAddress("karlcunanan@gmail.com")
            email.To.Add("karlcunanan@yahoo.com")
            email.Subject = "Test Email"
            email.Body = "Sample Message"
            Dim smtp As New SmtpClient("smtp.gmail.com")
            smtp.Port = 587
            smtp.EnableSsl = True
            smtp.Credentials = New System.Net.NetworkCredential("karlcunanan@gmail.com", "asdadsda")
            smtp.Send(email)

        Catch ex As Exception

        End Try
    End Sub
End Class

Edited 3 Years Ago by karlcunanan

Something like this might work. Of course I had to guess that all the values will easily convert to string. If they don't you'll have to add code to allow for that.

Imports System.Net.Mail
Public Class Form1
    Dim DataString As String
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        File.Create("C:\TestAttachment.csv")
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim email As New MailMessage()          
        Try
        For Each row As DataGridViewRow In DataGridView1.Rows
            DataString = ""
            For Each item As DataGridViewCell In row.Cells
                DataString += item.Value.ToString + ","
            Next
            DataString = DataString.Substring(0, DataString.Length - 1) + Environment.NewLine
        Next
        File.WriteAllText("C:\TestAttachment.csv", DataString)
        Dim newattachment As New Attachment("C:\TestAttachment.csv")
        email.Attachments.Add(newattachment)
        email.From = New MailAddress("karlcunanan@gmail.com")
        email.To.Add("karlcunanan@yahoo.com")
        email.Subject = "Test Email"
        email.Body = "Sample Message"
        Dim smtp As New SmtpClient("smtp.gmail.com")
        smtp.Port = 587
        smtp.EnableSsl = True
        smtp.Credentials = New System.Net.NetworkCredential("karlcunanan@gmail.com", "asdadsda")
        smtp.Send(email)
        Catch ex As Exception
        End Try
    End Sub
End Class

Edited 3 Years Ago by tinstaafl

Thanks for the response tinstaafl, but why am I getting this error? 'File' is not declared. It may be inaccessible due to its protection level. Is it only working in framework 4.5?

This article has been dead for over six months. Start a new discussion instead.