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

2
  • The first 2 options I think about are nested loops or Linq. What have you tried? Commented Nov 24, 2017 at 7:04
  • I have done this with datatables but am stuck with dataset Commented Nov 24, 2017 at 7:06

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

But how to add the returned html to the aspx page
Can you try creating div in aspx like <div id="divDynamicHTMLTable" runat="server"></div> 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
I tried this and it worked <asp:Label ID="lblResult" runat="server"/> and string result = ConvertDataSetTableToHTML(dsPNPrint.Tables[0]); lblResult.Text = result;
Glad to hear. Can you mark the solution as answered?
But the styles are not applying. Any idea why?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.