Sam Daniel
Sam Daniel

Reputation: 141

How to build a html table dynamically based on dataset

I have a dataset

DataSet ds = Access.Get(startdate, enddate);

I returns me about 70 rows.I need to build an html table based on this to provide printing option. Any suggestions on how to do this? Thanks

Upvotes: 1

Views: 1172

Answers (1)

Karthick Raju
Karthick Raju

Reputation: 797

Pass DataSet table to ConvertDataSetTableToHTML. Ensure your dataset always have table with records before calling.

if (ds != null)
{
    if (ds.Tables.Count >0 )
    {
        if (ds.Tables[0].Rows.Count > 0)
        {
            ConvertDataSetTableToHTML(ds.Tables[0]);
        }
    }
}
public static string ConvertDataSetTableToHTML(DataTable dt)
{
    string html = "<table>";
    //add header row
    html += "<tr>";
    for(int i=0;i<dt.Columns.Count;i++)
        html+="<td>"+dt.Columns[i].ColumnName+"</td>";
    html += "</tr>";
    //add rows
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        html += "<tr>";
        for (int j = 0; j< dt.Columns.Count; j++)
            html += "<td>" + dt.Rows[i][j].ToString() + "</td>";
        html += "</tr>";
    }
    html += "</table>";
    return html;
}

I just used the code from here for your scenario.

Upvotes: 1

Related Questions