Datatable to HTML

  • Thread starter Alphonse Giambrone
  • Start date
A

Alphonse Giambrone

Is there a simple way to convert the contents of a datatable to an HTML
table.
I have a dataset containing multiple datatables that I need to be able to
insert all the data into an html email.
I could loop through the rows of each datatable, but thought there might be
a simpler method.

TIA
 
K

Kevin Spencer

You can also use the lighter-weight DataList Control, if you don't need
paging.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
 
A

Alphonse Giambrone

Thanks for the quick reply Kevin.
I know how to use a datagrid and datalist to render to a webform.
What I would like to do is get the data into a string (formatted as an html
table) so that I can insert it into an email.
 
K

Kevin Spencer

DataGrid and DataList are System.Web.UI.Controls. What I mean by this is
that these are controls that render HTML in a web page. An email is not a
web page. However, an HTML email IS HTML, which is text. All you have to do
is write the HTML into the email message. But you won't be able to use any
system.Web.UI.Controls to do it.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
 
K

Kevin Spencer

BTW, my apologies for not noticing that your original message did say
"email." I wouldn't have mentioned the DataList if I had!

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
 
W

Weston Weems

if you dont need to present data in an email from a
website, I'd just suggest using a datareader, and a
stringbuilder.

Should be easy enough to
while(reader.Read()){ mystringbuilder.Append
("<tr><TD></td></tr>);

email.body = whatever;
email.body += mystringbuilder.ToString();
email.Send();
 
A

Alphonse Giambrone

Thanks for the reply Weston.
I am using a dataset because the source is a stored procedure that returns
multiple result sets via the SqlHelper application block. Each result set is
automatically output to a table within the data set.

I know how to loop through it all to build the string. I was just hoping
that there was something in the .NET framework that I missed to directly
output a datatable or dataset to html since it can be output to xml.
 
A

Alphonse Giambrone

No problem Kevin, I appreciate the response.

I am using a dataset because the source is a stored procedure that returns
multiple result sets via the SqlHelper application block. Each result set is
automatically output to a table within the data set.

I know how to loop through it all to build a string and put into the body
of an email. I was just hoping
that there was something in the .NET framework that I missed to directly
output a datatable or dataset to html since it can be output to xml.
 
S

Steven Cheng[MSFT]

Hi Alphonse,

I think if you want to get an Html table content from a given datatable,
generally we can consider either of the following means:
1. Since the System.Web.UI.WebControls.DataGrid can be created on the fly
and render out its html content.
We may manually create a DataGrid instance (set AutoGenerateColumns= true)
and bind the DataTable onto it , and call it's RenderControl method to
render into a HtmlTextWritter(created via a stringbuilder). After that , we
can get the datagrid's output html from the string builder. For example:

private void btnExport_Click(object sender, System.EventArgs e)
{
DataGrid dg = new DataGrid();
dg.AutoGenerateColumns = true;

dg.DataSource = GetDataTable();
dg.DataBind();

System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter sw= new System.IO.StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);

dg.RenderControl(htw);

divTable.InnerHtml = sb.ToString();

}

#sb.ToString() will return the htmltable content we want.

2. Put the DataTable into a DataSet and use the DataSet.GetXml method to
retreive the DataSet's xml representation. Then, use some custom xslt style
to transform the xml into html table content. This require us to provide an
xslt style sheet.

Hope these help. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
A

Alphonse Giambrone

Thanks Steven,

I have already written the routine to loop through the datatables to get
what I need, but I will certainly try what you propose as it seems like it
might have more flexibility for future use.
 
S

Steven Cheng[MSFT]

Thanks for your followup Alphonse,

If you have any further questions later, please feel free to post here.
Good luck!


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top