locked
Alternate row colors for dynamically built html table in C# RRS feed

  • Question

  • User-718146471 posted

    Currently, my table produces rows that are the same background color. I would like to have the header be Deep blue with White Text, then alternate the rows between white and grey with black text. Here is the code:

            protected string ExportDatatableToHtml(DataTable dt)
            {
                StringBuilder strHTMLBuilder = new StringBuilder();
                strHTMLBuilder.Append("<style type='text/css'>.TFtable{width:100%;border-collapse:collapse;}.TFtable 
    td{padding:7px;border:#4e95f4 1px solid;}.TFtable tr{background: #b8d1f3;}.TFtable tr:nth - child(odd){background:#b8d1f3;}.TFtable tr:nth -
    child(even){background: #dae5f4;}</style>"); strHTMLBuilder.Append("<table border='1px' cellpadding='1' cellspacing='1' class='TFtable' style='font-family:Arial;
    font-size:small;'>"); strHTMLBuilder.Append("<tr valign='top'>"); foreach (DataColumn myColumn in dt.Columns) { strHTMLBuilder.Append("<td><b>"); strHTMLBuilder.Append(myColumn.ColumnName); strHTMLBuilder.Append("</b></td>"); } strHTMLBuilder.Append("</tr>"); foreach (DataRow myRow in dt.Rows) { strHTMLBuilder.Append("<tr valign='top'>"); foreach (DataColumn myColumn in dt.Columns) { strHTMLBuilder.Append("<td>"); strHTMLBuilder.Append(myRow[myColumn.ColumnName].ToString()); strHTMLBuilder.Append("</td>"); } strHTMLBuilder.Append("</tr>"); } //Close tags. strHTMLBuilder.Append("</table>"); string Htmltext = strHTMLBuilder.ToString(); return Htmltext; }

    Monday, March 13, 2017 2:36 PM

Answers

  • User-1509636757 posted

    Check out code with similar requirement:

    protected string ExportDatatableToHtml(DataTable dt)
    {
        StringBuilder strHTMLBuilder = new StringBuilder();
        strHTMLBuilder.Append("<style type='text/css'>.TFtable{width:100%;border-collapse:collapse;}.TFtable td{padding:7px;border:#4e95f4 1px solid;}.TFtable tr{background: #b8d1f3;}.TFtable tr:nth - child(odd){background:#b8d1f3;}.TFtable tr:nth - child(even){background: #dae5f4;}</style>");
        strHTMLBuilder.Append("<table border='1px' cellpadding='1' cellspacing='1' class='TFtable' style='font-family:Arial; font-size:small;'>");
        strHTMLBuilder.Append("<tr valign='top'>");
    
        foreach (DataColumn myColumn in dt.Columns)
        {
            strHTMLBuilder.Append("<td><b>");
            strHTMLBuilder.Append(myColumn.ColumnName);
            strHTMLBuilder.Append("</b></td>");
        }
    
        strHTMLBuilder.Append("</tr>");
    
        int currRow = 1;
        foreach (DataRow myRow in dt.Rows)
        {
                    
            strHTMLBuilder.Append("<tr style='" + (currRow % 2 == 0 ? "background-color:white" : "background-color:silver") + "' valign='top'>");
            foreach (DataColumn myColumn in dt.Columns)
            {
                strHTMLBuilder.Append("<td>");
                strHTMLBuilder.Append(myRow[myColumn.ColumnName].ToString());
                strHTMLBuilder.Append("</td>");
    
            }
            strHTMLBuilder.Append("</tr>");
            currRow++;
        }
    
        //Close tags.   
        strHTMLBuilder.Append("</table>");
        string Htmltext = strHTMLBuilder.ToString();
    
        return Htmltext;
    }

    Output:

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 13, 2017 2:56 PM