Results 1 to 21 of 21

Thread: Vb.net send emails with html colors cells table

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2022
    Posts
    11

    Vb.net send emails with html colors cells table

    Hello everyone, I'm new to the forum and to vb.net programming. So I apologize if I'm not in the right place or ask trivial questions.
    My problem is being able to color some cells of an html table in outlook.
    I have a dgv compiled with data manually once compiled I should send it by email. Now I managed to fill in the table and convert it to html and insert it in outlook but I can't color the cells, I can only color the whole row the same color, but I only need some cells and different colors. Thank you all.

    Name:  dgv.jpg
Views: 267
Size:  10.0 KB - Name:  Email.jpg
Views: 235
Size:  8.9 KB[QUOTE]


    This is my code.

    Code:
     strB.AppendLine("<html><head><table, th, td {border: 1px solid black;border-collapse: collapse}</style></head><body><h2>" & "Test " & Form1.ComboBox1.Text & " " & Form1.ComboBox3.Text + "</h2>")
            strB.AppendLine("<html><body><" & "table border='2' cellpadding='2' cellspacing='0' style=width:100% >")
            strB.AppendLine("<tr>")
    
         
            For value As Integer = 0 To Mydgv.Columns.Count - 1
                strB.AppendLine("<td align='center' valign='middle' style='background-color:DodgerBlue'> " & Mydgv.Columns(value).HeaderText + "</td>")
            Next
            strB.AppendLine("<tr>")
            For value As Integer = 0 To Report.Rows.Count - 1
                For Each dgvc As DataGridViewCell In Mydgv.Rows(value).Cells
                    strB.AppendLine("<td align='center' valign='middle'>" & dgvc.Value.ToString() & "</td>")
                Next
                strB.AppendLine("</tr>")
            Next
    Last edited by Marco:G; Nov 25th, 2022 at 08:31 AM.

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,660

    Re: Vb.net send emails with html colors cells table

    Hi, welcome to the forum. I can't answer your question, but if you post code here you should enclose it in code tags using the "#" button in the toolbar of the message you're about to submit.

    Also, looking at your code, you might want to read this page: https://learn.microsoft.com/en-us/do...olated-strings

  3. #3
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,489

    Re: Vb.net send emails with html colors cells table

    In this part you need some code to check the color of the grid cell and add this color to the cell in the HTML
    Code:
            For value As Integer = 0 To Report.Rows.Count - 1
                For Each dgvc As DataGridViewCell In Mydgv.Rows(value).Cells
    ' You need to add the background color in this part, like style='background-color:DodgerBlue'
                    strB.AppendLine("<td align='center' valign='middle'>" & dgvc.Value.ToString() & "</td>")
                Next
                strB.AppendLine("</tr>")
            Next

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2022
    Posts
    11

    Re: Vb.net send emails with html colors cells table

    Thanks, I fixed the code (#), I'm looking at the link you sent me but I can't find a solution.
    Last edited by Marco:G; Oct 26th, 2022 at 04:14 AM.

  5. #5
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,660

    Re: Vb.net send emails with html colors cells table

    @Margo:G - my link doesn't provide the answer to your question. It contains information about how to work with strings. You shouldn't use the concatenation operator ("&") to combine strings if it can be avoided.

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2022
    Posts
    11

    Re: Vb.net send emails with html colors cells table

    Quote Originally Posted by Peter Swinkels View Post
    @Margo:G - my link doesn't provide the answer to your question. It contains information about how to work with strings. You shouldn't use the concatenation operator ("&") to combine strings if it can be avoided.
    Thank you so much for the answers you give me and for the inconvenience I cause.
    But as I said before I'm just starting out with this type of programming, I'm reading on the web and hanging as much as I can.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,059

    Re: Vb.net send emails with html colors cells table

    Instead of style='background-colorodgerBlue'

    Try…

    <td bgcolor= "DodgerBlue” ‘etc

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2022
    Posts
    11

    Re: Vb.net send emails with html colors cells table

    Quote Originally Posted by Arnoutdv View Post
    In this part you need some code to check the color of the grid cell and add this color to the cell in the HTML
    Code:
            For value As Integer = 0 To Report.Rows.Count - 1
                For Each dgvc As DataGridViewCell In Mydgv.Rows(value).Cells
    ' You need to add the background color in this part, like style='background-color:DodgerBlue'
                    strB.AppendLine("<td align='center' valign='middle'>" & dgvc.Value.ToString() & "</td>")
                Next
                strB.AppendLine("</tr>")
            Next
    Code:
    For value As Integer = 0 To Mydgv.Rows.Count - 1
                For Each dgvc As DataGridViewCell In Mydgvc.Rows(value).Cells
                    Dim cellcolor = dgvc.Style.BackColor
                    strB.AppendFormat("<td align='center' valign='middle' bgcolor = " + cellcolor.Name + " > {0}</td>", dgvc.Value.ToString)
                Next
                strB.AppendLine("</tr>")
            Next
    Ok, in the meantime, thanks to everyone.
    I took a step forward, now I can color the last cell of each row (red or green) as needed. But not the others.
    Anyone know where I'm still wrong?

  9. #9
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,489

    Re: Vb.net send emails with html colors cells table

    Maybe the cell color names from the DataGrid are not valid HTML color opcodes?

  10. #10

    Thread Starter
    New Member
    Join Date
    Oct 2022
    Posts
    11

    Re: Vb.net send emails with html colors cells table

    Quote Originally Posted by Arnoutdv View Post
    Maybe the cell color names from the DataGrid are not valid HTML color opcodes?
    No, I tried to put the same color as the last cell but only color that one.

  11. #11
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,489

    Re: Vb.net send emails with html colors cells table

    What is the output for the first few lines?

  12. #12

    Thread Starter
    New Member
    Join Date
    Oct 2022
    Posts
    11

    Re: Vb.net send emails with html colors cells table

    Quote Originally Posted by Arnoutdv View Post
    What is the output for the first few lines?
    output? I do not understand.
    the 1st column is white
    the 2nd changes according to a value
    the 3 - 4 - 5 always gray
    the latter changes according to the internal value
    And all of the lines are compiled in the dgv by the user one at a time

  13. #13
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,489

    Re: Vb.net send emails with html colors cells table

    The content of strB
    Then you (and we) can check what is written in the text aka the <td> .. </td>

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,059

    Re: Vb.net send emails with html colors cells table

    Try this...

    Code:
    strB.AppendFormat("<td align=""center"" valign=""middle"" bgcolor=""{0}"" >{1}</td>", cellcolor.Name, dgvc.Value.ToString)

  15. #15

    Thread Starter
    New Member
    Join Date
    Oct 2022
    Posts
    11

    Re: Vb.net send emails with html colors cells table

    Quote Originally Posted by .paul. View Post
    Try this...

    Code:
    strB.AppendFormat("<td align=""center"" valign=""middle"" bgcolor=""{0}"" >{1}</td>", cellcolor.Name, dgvc.Value.ToString)

    Good Morning everyone.
    no, unfortunately it always colors the last cell

  16. #16
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,489

    Re: Vb.net send emails with html colors cells table

    Then please show the content of the strB.
    Maybe the content is correct and Outlook is dealing strange with.

  17. #17

    Thread Starter
    New Member
    Join Date
    Oct 2022
    Posts
    11

    Re: Vb.net send emails with html colors cells table

    Quote Originally Posted by Arnoutdv View Post
    The content of strB
    Then you (and we) can check what is written in the text aka the <td> .. </td>
    Code:
    strB.AppendFormat("{<html><head><table, th, td {border: 1px solid black;border-collapse: collapse}</style></head><body><h2>Rapportino 1 a</h2>\r\n<html><body><table border='2' cellpadding='2' cellspacing='0' style=width:100% >\r\n<tr>\r\n<td align='center' valign='middle' style='background-color:DodgerBlue'> Q</td>\r\n<td align='center' valign='middle' style='background-color:DodgerBlue'> U</td>\r\n<td align='center' valign='middle' style='background-color:DodgerBlue'> E</td>\r\n<td align='center' valign='middle' style='background-color:DodgerBlue'> R</td>\r\n<td align='center' valign='middle' style='background-color:DodgerBlue'> T</td>\r\n<td align='center' valign='middle' style='background-color:DodgerBlue'> Y</td>\r\n<tr>\r\n}")
    Attachment 186069

  18. #18

    Thread Starter
    New Member
    Join Date
    Oct 2022
    Posts
    11

    Re: Vb.net send emails with html colors cells table

    Quote Originally Posted by Marco:G View Post
    Code:
    strB.AppendFormat("{<html><head><table, th, td {border: 1px solid black;border-collapse: collapse}</style></head><body><h2>Rapportino 1 a</h2>\r\n<html><body><table border='2' cellpadding='2' cellspacing='0' style=width:100% >\r\n<tr>\r\n<td align='center' valign='middle' style='background-color:DodgerBlue'> Q</td>\r\n<td align='center' valign='middle' style='background-color:DodgerBlue'> U</td>\r\n<td align='center' valign='middle' style='background-color:DodgerBlue'> E</td>\r\n<td align='center' valign='middle' style='background-color:DodgerBlue'> R</td>\r\n<td align='center' valign='middle' style='background-color:DodgerBlue'> T</td>\r\n<td align='center' valign='middle' style='background-color:DodgerBlue'> Y</td>\r\n<tr>\r\n}")
    Attachment 186069
    Sorry, wrong code!!!!
    this is correct.
    Code:
    strB.AppendFormat("{<html><head><table, th, td {border: 1px solid black;border-collapse: collapse}</style></head><body><h2>Rapportino 1 a</h2>\r\n<html><body><table border='2' cellpadding='2' cellspacing='0' style=width:100% >\r\n<tr>\r\n<td align='center' valign='middle' style='background-color:DodgerBlue'> Q</td>\r\n<td align='center' valign='middle' style='background-color:DodgerBlue'> U</td>\r\n<td align='center' valign='middle' style='background-color:DodgerBlue'> E</td>\r\n<td align='center' valign='middle' style='background-color:DodgerBlue'> R</td>\r\n<td align='center' valign='middle' style='background-color:DodgerBlue'> T</td>\r\n<td align='center' valign='middle' style='background-color:DodgerBlue'> Y</td>\r\n<tr>\r\n<td align="center" valign="middle" bgcolor= 0> 1</td><td align="center" valign="middle" bgcolor= 0> a</td><td align="center" valign="middle" bgcolor= 0> DDDDDD</td><td align="center" valign="middle" bgcolor= 0> GGGGGGGGG</td><td align="center" valign="middle" bgcolor= 0> MMMMMMMMMMMM</td><td align="center" valign="middle" bgcolor= Red> SOSPESA</td></tr>\r\n<td align="center" valign="middle" bgcolor= 0> </td><td align="center" valign="middle" bgcolor= 0> </td><td align="center" valign="middle" bgcolor= 0> PPPPP</td><td align="center" valign="middle" bgcolor= 0> UUUUUUUU</td><td align="center" valign="middle" bgcolor= 0> PPPPPPPPPPP</td><td align="center" valign="middle" bgcolor= Green> FINITA</td></tr>\r\n</table></center></body></html>\r\n}")
    Attachment 186070 - Attachment 186071

  19. #19

    Thread Starter
    New Member
    Join Date
    Oct 2022
    Posts
    11

    Re: Vb.net send emails with html colors cells table

    OK, thank you all for the suggestions which have all been helpful to me. I solved. I was working wrong in another part of the program.

    wrong, this way it does not copy the colors
    Code:
    Mydgv.Columns(2).DefaultCellStyle.BackColor = Color.LightGray
    in this way, yes
    Code:
    .Item(2, e.RowIndex).Style.BackColor = Color.LightGray

    thank you all

  20. #20
    New Member
    Join Date
    Oct 2022
    Location
    Los Ángeles, California
    Posts
    1

    Re: [RESOLVED] Vb.net send emails with html colors cells table

    Very helpul. Thank you all!

  21. #21

    Thread Starter
    New Member
    Join Date
    Oct 2022
    Posts
    11

    Re: [RESOLVED] Vb.net send emails with html colors cells table

    I take up again this post that I had opened to ask for one more information.
    Is there a possibility to have different alignments within a stringbuilder?
    Let me explain: I have my table with 6 columns and number of rows which depends on how it is compiled as you can see from the photo above. I had problems with coloring but you helped me to solve it and everything is ok, I was wondering if it was possible to have the cell values with the same alignment as the datagridview. Or if it were possible for example to have only the cell of column 5 with left alignment while all the other cells of the other columns centered.

    This is the part of code
    Code:
    For value As Integer = 0 To dgv.Rows.Count - 1
                For Each dgvc As DataGridViewCell In dgv.Rows(value).Cells
                    Dim cellcolor = dgvc.Style.BackColor
                    strB.AppendFormat("<td align=""center"" valign=""middle"" style=""font-family:Microsoft JhengHei;font-size:14"" bgcolor= " + cellcolor.Name + ">{0}</td>", dgvc.Value.ToString)
                          Next
                strB.AppendLine("</tr>")
            Next

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width