1

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

Sam Daniel
  • 141
  • 1
  • 3
  • 17

1 Answers1

1

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.

Karthick Raju
  • 757
  • 8
  • 29
  • But how to add the returned html to the aspx page – Sam Daniel Nov 24 '17 at 07:09
  • Can you try creating `div` in aspx like `
    ` and in code behind file, `divDynamicHTMLTable.InnerHtml= ConvertDataSetTableToHTML(ds.Tables[0]);`? Place code behind change inside if condition by replacing `ConvertDataSetTableToHTML(ds.Tables[0]);`. I don't have VS with me at the moment, so try and let me know
    – Karthick Raju Nov 24 '17 at 07:16
  • I tried this and it worked and string result = ConvertDataSetTableToHTML(dsPNPrint.Tables[0]); lblResult.Text = result; – Sam Daniel Nov 24 '17 at 07:23
  • Glad to hear. Can you mark the solution as answered? – Karthick Raju Nov 24 '17 at 07:32
  • But the styles are not applying. Any idea why? – Sam Daniel Nov 24 '17 at 07:39
  • Styles? Can you create a new post with screenshot? This would help to understand. Also, post the code created for style – Karthick Raju Nov 24 '17 at 07:47