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.

I found this ebook very helpful, it has a chapter on how to send email

@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

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

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?

Sorry forgot to include Imports System.IO

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.