I had a data table with columns and under column result values will be yes and no if it is yes I need to mark green ,if no I need color red for that particular cells I need to create html table for this
Follow these steps.
- Loop through the DataTable: Iterate over each row and column.
- Check Value Conditions: For each cell, check if the value is “Yes” or “No”.
- Generate HTML Code: Based on the condition, set the background color of the cell to green (for “Yes”) or red (for “No”).
Code outline like this.
' Start building the HTML table
htmlString = "<table border='1'>"
' Add header row
htmlString &= "<tr>"
For Each column As DataColumn In yourDataTable.Columns
htmlString &= "<th>" & column.ColumnName & "</th>"
Next
htmlString &= "</tr>"
' Add data rows
For Each row As DataRow In yourDataTable.Rows
htmlString &= "<tr>"
For Each cell As Object In row.ItemArray
If cell.ToString().Equals("Yes") Then
htmlString &= "<td bgcolor='green'>" & cell.ToString() & "</td>"
ElseIf cell.ToString().Equals("No") Then
htmlString &= "<td bgcolor='red'>" & cell.ToString() & "</td>"
Else
htmlString &= "<td>" & cell.ToString() & "</td>"
End If
Next
htmlString &= "</tr>"
Next
' End the table HTML
htmlString &= "</table>"
Hello @Venkat5,
Use this code:-
Colored Table table { width: 50%; border-collapse: collapse; } th, td { border: 1px solid #ddd; text-align: center; padding: 8px; } .yes { background-color: green; color: white; } .no { background-color: red; color: white; }| Name | Result |
|---|---|
| John | Yes |
| Jane | No |
| Sam | Yes |
| Anna | No |
Output:-
Hey @Venkat5 are you trying to do something like this to check if value is Yes then Fill with green or else Red then here is the solution.
and this Index is a Int Variable that i created in for each row
cheers
Thanks @ashokkarale this approach works.
1 Like
Glad to hear @Venkat5.
Kindly close the thread by marking the answer as solution.
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.




