mercredi 3 mars 2021

Generate html table with multiple row headers using DataTable (ASP.NET MVC Web Application)

i have this method to cast my DataTable object to an html string:

DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[4]
{
                new DataColumn("Descrizione investimento",typeof(string)),
                new DataColumn("Data effettuazione",typeof(string)),
                new DataColumn("Importo",typeof(string)),
                new DataColumn("Caratteristiche operative",typeof(string)),
});

foreach (FinanziamentoDaTerziResultModel finanziamentoDaTerzi in finanziamentiDaTerziResults)
{
    dt.Rows.Add(finanziamentoDaTerzi.Descrizione, finanziamentoDaTerzi.Data_Erogazione.ToString("dd/MM/yyyy"), finanziamentoDaTerzi.Importo.ToString(CultureInfo.CurrentCulture), "Testo");
}

StringBuilder sb = new StringBuilder();
// Table start
sb.Append("<table cellpadding='5' cellspacing='0' style='border: 1px solid #ccc;font-size: 9pt;font-family:Arial' width='100%'>");

// adding HeaderRow
sb.Append("<tr>");
foreach (DataColumn column in dt.Columns)
{
    sb.Append("<th style='height: 30px;background-color: #ffffff;border-top: 1px solid #a5a5a5;border-bottom: 1px solid #a5a5a5;color: #000000;font-weight: bold;'>").Append(column.ColumnName).Append("</th>");
}

sb.Append("</tr>");

// adding DataRow
int index = 0;
foreach (DataRow row in dt.Rows)
{
    string trColor;
    if (index == 0)
        trColor = "#ededed";
    else
        trColor = "#ffffff";

    sb.Append("<tr style='background-color: ").Append(trColor).Append(";height: 35px;'>");
    foreach (DataColumn column in dt.Columns)
    {
        sb.Append("<td style='width:100px;border: 1px solid #ccc;'>").Append(row[column.ColumnName].ToString()).Append("</td>");
    }
    sb.Append("</tr>");

    if (index == 0)
        index = 1;
    else
        index = 0;
}

// Table end
sb.Append("</table>");

return sb.ToString();

It works correctly now, but I need to create a table that has parent columns in which there must be sub-columns referring to the element defined in the parent column.

This is an example of what it should look like:

+----------+--------------------------------+--------------------------------+
|Name      |Year 1                          |Year 2                          |
|          |----------+----------+----------+----------+----------+----------+
|          |1st Term  |2nd Term  |3rd Term  |1st Term  |2nd Term  |3rd Term  |    
+----------+----------+----------+----------+----------+----------+----------+
|Name1     |XX        |YY        |ZZ        |XX        |YY        |ZZ        |
+----------+----------+----------+----------+----------+----------+----------+
|Name2     |YY        |ZZ        |XX        |YY        |ZZ        |XX        |
+----------+----------+----------+----------+----------+----------+----------+

Is it possible to achieve this by using DataTable objects? And if so how can I do?

(note this is a web application, not a desktop app so i'haven't gridview or other Window.Forms tools)

Thanks a lot




Aucun commentaire:

Enregistrer un commentaire