-
Oct 26th, 2022, 01:56 AM
#1
Thread Starter
New Member
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.
- [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.
-
Oct 26th, 2022, 03:31 AM
#2
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
-
Oct 26th, 2022, 04:01 AM
#3
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
-
Oct 26th, 2022, 04:07 AM
#4
Thread Starter
New Member
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.
-
Oct 26th, 2022, 04:25 AM
#5
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.
-
Oct 26th, 2022, 05:03 AM
#6
Thread Starter
New Member
Re: Vb.net send emails with html colors cells table
 Originally Posted by Peter Swinkels
@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.
-
Oct 26th, 2022, 05:36 AM
#7
Re: Vb.net send emails with html colors cells table
Instead of style='background-color odgerBlue'
Try…
<td bgcolor= "DodgerBlue” ‘etc
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 26th, 2022, 06:30 AM
#8
Thread Starter
New Member
Re: Vb.net send emails with html colors cells table
 Originally Posted by Arnoutdv
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?
-
Oct 26th, 2022, 06:39 AM
#9
Re: Vb.net send emails with html colors cells table
Maybe the cell color names from the DataGrid are not valid HTML color opcodes?
-
Oct 26th, 2022, 06:47 AM
#10
Thread Starter
New Member
Re: Vb.net send emails with html colors cells table
 Originally Posted by Arnoutdv
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.
-
Oct 26th, 2022, 07:35 AM
#11
Re: Vb.net send emails with html colors cells table
What is the output for the first few lines?
-
Oct 26th, 2022, 07:44 AM
#12
Thread Starter
New Member
Re: Vb.net send emails with html colors cells table
 Originally Posted by Arnoutdv
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
-
Oct 26th, 2022, 08:25 AM
#13
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>
-
Oct 26th, 2022, 03:42 PM
#14
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)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 27th, 2022, 02:21 AM
#15
Thread Starter
New Member
Re: Vb.net send emails with html colors cells table
 Originally Posted by .paul.
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
-
Oct 27th, 2022, 02:25 AM
#16
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.
-
Oct 27th, 2022, 02:37 AM
#17
Thread Starter
New Member
Re: Vb.net send emails with html colors cells table
 Originally Posted by Arnoutdv
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
-
Oct 27th, 2022, 02:45 AM
#18
Thread Starter
New Member
Re: Vb.net send emails with html colors cells table
 Originally Posted by Marco:G
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
-
Oct 27th, 2022, 03:43 AM
#19
Thread Starter
New Member
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
-
Oct 28th, 2022, 10:10 AM
#20
New Member
Re: [RESOLVED] Vb.net send emails with html colors cells table
Very helpul. Thank you all!
-
Nov 25th, 2022, 08:28 AM
#21
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|