Alternate row colors

Padmanabhan, Venkatesh 246 Reputation points
2021-08-11T11:10:13.47+00:00

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

Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Adnan Dedic 406 Reputation points
    2021-08-11T19:35:17.617+00:00

    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

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.