question

PadmanabhanVenkatesh-6789 avatar image
0 Votes"
PadmanabhanVenkatesh-6789 asked AdnanDedic edited

Alternate row colors

Hi.
I have a data table from which I am trying to create a html table , which can be send in email.

I am able to get the data generated and mail being send, but the styles are not getting applied properly.

The table column names are different from the data column names. I am not able to add the html script tags here. Code is from one of the answers in : https://forums.asp.net/t/2117351.aspx?Alternate+row+colors+for+dynamically+built+html+table+in+C+

I have not used the first foreach loop , which loops the column names. I have manually put names for the initial TR.

  int currRow = 1;
               
                                       
             for (int loopCount = 0; loopCount < ds.Rows.Count; loopCount++)
                 {
                     mailBody.Append(
                         "<td>" + ds.Rows[loopCount]["FILE_NAME"] + " : " + ds.Rows[loopCount]["DESCRIPTION"] + "</td>" +
                         "<td>" + ds.Rows[loopCount]["FILE_DATE"] + "</td>" +
                         "<td>" + ds.Rows[loopCount]["PERIOD"] + "</td>" )  ;
                 mailBody.Append("</tr>");
                  
             }
    
             mailBody.Append("</table>");

How to get the alternate colors for the rows ? Thanks

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AdnanDedic avatar image
1 Vote"
AdnanDedic answered AdnanDedic edited

Hi,

The line where HTML table row is added and styled is missing.
Please add the following line to the line 5:

 mailBody.Append("<tr style='" + (loopCount % 2 == 0 ? "background-color:white" : "background-color:lightblue") + "' valign='top'>");

Example:
122466-rowalternationcode-capture.jpg

Result:
122491-rowalternation-capture.jpg

BR,
Adnan



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.