Question Fetch items from Datagridview to Attachment

luckydead

New member
Joined
Feb 3, 2022
Messages
4
Programming Experience
1-3
Hello can someone help me,
How i can fetch the items from datagridview to attach them to email
How it will be in function SendMail()

Can someone tell me is it correct now , how to read files from Datagridview in attachment
VB.NET:
Public Sub SendMail()
        Dim oEmail As MailItem = CType(New Application().CreateItem(OlItemType.olMailItem), MailItem)
        oEmail.Recipients.Add("emailTo") 'Send TO
        oEmail.CC = "CopyEmail" 'Send CC
        oEmail.Recipients.ResolveAll()
        oEmail.Subject = "Spam - Meow!"
        oEmail.BodyFormat = OlBodyFormat.olFormatHTML
        oEmail.Body = "Blah, blah, blah..."
        oEmail.Importance = OlImportance.olImportanceNormal  'Request read email confirmation
        oEmail.ReadReceiptRequested = True
     
       If DataGridView1.Rows.Count > 0 Then
            For Each row As DataGridViewRow In DataGridView1.Rows
                If row.Cells("Name").Tag IsNot Nothing Then
                    Dim MsgAttach As New Attachment(CStr(row.Cells("Name").Tag))
                    oEmail.Attachments.Add(MsgAttach)
                End If
            Next
        End If

        oEmail.Recipients.ResolveAll()
        oEmail.Save()
        oEmail.Display(False) 'Show the email message and allow for editing before sending
        oEmail.Send() 'You can automatically send the email without displaying it.
    End Sub
Button Browse:
VB.NET:
OpenFileDialog1.FileName = "Folder1"
        OpenFileDialog1.DefaultExt = "*.*"
        OpenFileDialog1.FilterIndex = 6
        OpenFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
        OpenFileDialog1.Multiselect = True
        Dim saveDirectory As String = ".\Files\"
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            If Not Directory.Exists(saveDirectory) Then
                Directory.CreateDirectory(saveDirectory)
            End If
            For Each item As String In OpenFileDialog1.FileNames
                Dim fileName As String = Path.GetFileName(OpenFileDialog1.FileName)
                Dim fileSavePath As String = Path.Combine(saveDirectory, fileName)
                File.Copy(OpenFileDialog1.FileName, fileSavePath, True)
            Next
        End If
        BindDataGrid()

VB.NET:
Private Sub BindDataGrid()
        Dim filePaths As String() = Directory.GetFiles(".\Files\")
        Dim dt As DataTable = New DataTable()
        dt.Columns.Add("Text")
        dt.Columns.Add("Value")

        For Each filePath As String In filePaths
            dt.Rows.Add(IO.Path.GetFileName(filePath), filePath)
        Next

        dataGridView1.AllowUserToAddRows = False
        dataGridView1.Columns.Clear()
        Dim name As DataGridViewColumn = New DataGridViewTextBoxColumn With {
            .Name = "Name",
            .HeaderText = "File Name",
            .DataPropertyName = "Text",
            .Width = 100
        }
        DataGridView1.Columns.Insert(0, name)
        Dim path As DataGridViewColumn = New DataGridViewTextBoxColumn With {
            .Name = "Path",
            .HeaderText = "File Path",
            .DataPropertyName = "Value",
            .Width = 100
        }
        DataGridView1.Columns.Insert(1, path)
        DataGridView1.DataSource = dt
        Dim buttonColumn As DataGridViewButtonColumn = New DataGridViewButtonColumn With {
            .HeaderText = "",
            .Width = 60,
            .Name = "buttonColumn",
            .Text = "Delete",
            .UseColumnTextForButtonValue = True
        }
        DataGridView1.Columns.Insert(2, buttonColumn)
    End Sub

VB.NET:
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        If e.ColumnIndex = 2 Then
            Dim row As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
            If MessageBox.Show(String.Format("Do you want to delete Name:{0}?", row.Cells("Name").Value), "Confirmation", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                File.Delete(row.Cells("Path").Value.ToString())
                BindDataGrid()
            End If
        End If
    End Sub

Is it correct to attach the files from the datagridview to the email ?
 
You don't need to ask us whether code you have is correct. You can test it for yourself and, if it works, it is correct. If it doesn't work, you can debug it to see where it behaves other than you expect. If you need help, you can describe to us what happened when you tested/debugged. So, did the code do what you expected when you ran it? If not, exactly where and how did it not behave as expected?
 
i do not ask is it working the code, i was asking to check function is it correct how i made it to get the datagridview items, anyway i fixed it
 
i do not ask is it working the code, i was asking to check function is it correct
They are the same thing. If it works it is correct and if it doesn't it's not. There's not one way to do something. If you "fixed" it then presumably it didn't work, so it wasn't correct, but you gave us no indication of what was wrong with it. You need to do appropriate testing/debugging first and then explain to us what you found.
 
Back
Top