FYI, below is one way to convert a DataTable to HTML.
Here is the code...
/// <summary> /// This is a simple way to convert a DataTable to an HTML file. /// </summary> /// <param name="targetTable">This the table to convert.</param> /// <returns>This is the HTML output, which can saved as a file.</returns> public static string ConvertToHtmlFile(DataTable targetTable) { string myHtmlFile = "";
if (targetTable == null) { throw new System.ArgumentNullException("targetTable"); } else { //Continue. }
//Get a worker object. StringBuilder myBuilder = new StringBuilder();
//Open tags and write the top portion. myBuilder.Append("<html xmlns='http://www.w3.org/1999/xhtml'>"); myBuilder.Append("<head>"); myBuilder.Append("<title>"); myBuilder.Append("Page-"); myBuilder.Append(Guid.NewGuid().ToString()); myBuilder.Append("</title>"); myBuilder.Append("</head>"); myBuilder.Append("<body>"); myBuilder.Append("<table border='1px' cellpadding='5' cellspacing='0' "); myBuilder.Append("style='border: solid 1px Silver; font-size: x-small;'>");
Why not bind the dataTable into a GridView or DataGrid so will get the result on the web page that you are looking for
My requirements called for getting an entire, valid, text for an HTML page in a string, and then another process takes that and creates an actual, physical HTML file on disk.
I started looked at making a GridView in-memory, streaming it to TextWriter or HtmlWriter, getting that text, adding the HTML opening and closing tags, etc-- but, that was more work (or at least the same amount of work), than just looping and doing simple
string manipulation, IMHO.
I am sure binding to a GridView and outputting that could work-- but, I am not convinced that it is actuallly more simple than what I have shown when the target requirement is as stated above.
That said, I would be happy to see any other code that can do what I need (get a whole HTML page as a string) via other methods.
Well you can always create a control inherit from compositecontrol and have a gridview as a private member binding it on the constructor and render OnRender using the HtmlWriter, that'll will fullfil your requierement I believe.
Well you can always create a control inherit from compositecontrol and have a gridview as a private member binding it on the constructor and render OnRender using the HtmlWriter, that'll will fullfil your requierement I believe.
Well you can always create a control inherit from compositecontrol and have a gridview as a private member binding it on the constructor and render OnRender using the HtmlWriter, that'll will fullfil your requierement I believe.
You don't fancy giving us an example of doing this do you? And if you are in a real good mood, how about in ASP.NET using VB as I don't use C# yet...??
...how about in ASP.NET using VB as I don't use C# yet...??
FWIW, here is a VB version of my C# sample above...
''' This provides a simple way to convert a DataTable to an HTML file.
''' </summary>
''' <param name="targetTable">This the table to convert.</param>
''' <returns>This is the HTML output, which can saved as a file.</returns>
Public Shared Function ConvertToHtmlFile(ByVal targetTable As DataTable) As String
Dim myHtmlFile As String = ""
If (targetTable Is Nothing) Then
Throw New System.ArgumentNullException("targetTable")
Else
'Continue.
End If
'Get a worker object.
Dim myBuilder As System.Text.StringBuilder = New System.Text.StringBuilder()
'Open tags and write the top portion.
myBuilder.Append("<html xmlns='http://www.w3.org/1999/xhtml'>")
myBuilder.Append("<head>")
myBuilder.Append("<title>")
myBuilder.Append("Page-")
myBuilder.Append(Guid.NewGuid().ToString())
myBuilder.Append("</title>")
myBuilder.Append("</head>")
myBuilder.Append("<body>")
myBuilder.Append("<table border='1px' cellpadding='5' cellspacing='0' ")
myBuilder.Append("style='border: solid 1px Silver; font-size: x-small;'>")
'Add the headings row.
myBuilder.Append("<tr align='left' valign='top'>")
For Each myColumn As DataColumn In targetTable.Columns
myBuilder.Append("<td align='left' valign='top'>")
myBuilder.Append(myColumn.ColumnName)
myBuilder.Append("</td>")
Next myColumn
myBuilder.Append("</tr>")
'Add the data rows.
For Each myRow As DataRow In targetTable.Rows
myBuilder.Append("<tr align='left' valign='top'>")
For Each myColumn As DataColumn In targetTable.Columns
myBuilder.Append("<td align='left' valign='top'>")
myBuilder.Append(myRow(myColumn.ColumnName).ToString())
myBuilder.Append("</td>")
Next myColumn
myBuilder.Append("</tr>")
Next myRow
'Close tags.
myBuilder.Append("</table>")
myBuilder.Append("</body>")
myBuilder.Append("</html>")
'Get the string for return.
myHtmlFile = myBuilder.ToString()
Return myHtmlFile
End Function
mkamoski
Contributor
5694 Points
1565 Posts
convert a DataTable to HTML
Mar 30, 2007 08:14 PM|LINK
FYI, below is one way to convert a DataTable to HTML.
Here is the code...
HTH.
Thank you.
-- Mark Kamoski
albertpascua...
All-Star
17520 Points
3475 Posts
MVP
Re: convert a DataTable to HTML
Mar 31, 2007 03:24 PM|LINK
Mark,
Why not bind the dataTable into a GridView or DataGrid so will get the result on the web page that you are looking for
Al
My Blog
mkamoski
Contributor
5694 Points
1565 Posts
Re: convert a DataTable to HTML
Apr 01, 2007 12:56 AM|LINK
My requirements called for getting an entire, valid, text for an HTML page in a string, and then another process takes that and creates an actual, physical HTML file on disk.
I started looked at making a GridView in-memory, streaming it to TextWriter or HtmlWriter, getting that text, adding the HTML opening and closing tags, etc-- but, that was more work (or at least the same amount of work), than just looping and doing simple string manipulation, IMHO.
I am sure binding to a GridView and outputting that could work-- but, I am not convinced that it is actuallly more simple than what I have shown when the target requirement is as stated above.
That said, I would be happy to see any other code that can do what I need (get a whole HTML page as a string) via other methods.
Thank you.
-- Mark Kamoski
albertpascua...
All-Star
17520 Points
3475 Posts
MVP
Re: convert a DataTable to HTML
Apr 01, 2007 02:47 PM|LINK
Al
My Blog
Atanu
Member
2 Points
1 Post
Re: convert a DataTable to HTML
Sep 21, 2007 09:15 AM|LINK
I need to do the same in a windows service. can i do the same using a datagrid??
GeorgeZ
Contributor
2325 Points
552 Posts
Re: convert a DataTable to HTML
Sep 28, 2007 04:02 AM|LINK
Good Idea.
dotnet_lee
Member
467 Points
207 Posts
Re: convert a DataTable to HTML
Oct 05, 2007 08:13 AM|LINK
You don't fancy giving us an example of doing this do you? And if you are in a real good mood, how about in ASP.NET using VB as I don't use C# yet...??
mkamoski
Contributor
5694 Points
1565 Posts
Re: convert a DataTable to HTML
Oct 31, 2007 02:10 AM|LINK
FWIW, here is a VB version of my C# sample above...
''' This provides a simple way to convert a DataTable to an HTML file. ''' </summary> ''' <param name="targetTable">This the table to convert.</param> ''' <returns>This is the HTML output, which can saved as a file.</returns> Public Shared Function ConvertToHtmlFile(ByVal targetTable As DataTable) As String Dim myHtmlFile As String = "" If (targetTable Is Nothing) Then Throw New System.ArgumentNullException("targetTable") Else 'Continue. End If 'Get a worker object. Dim myBuilder As System.Text.StringBuilder = New System.Text.StringBuilder() 'Open tags and write the top portion. myBuilder.Append("<html xmlns='http://www.w3.org/1999/xhtml'>") myBuilder.Append("<head>") myBuilder.Append("<title>") myBuilder.Append("Page-") myBuilder.Append(Guid.NewGuid().ToString()) myBuilder.Append("</title>") myBuilder.Append("</head>") myBuilder.Append("<body>") myBuilder.Append("<table border='1px' cellpadding='5' cellspacing='0' ") myBuilder.Append("style='border: solid 1px Silver; font-size: x-small;'>") 'Add the headings row. myBuilder.Append("<tr align='left' valign='top'>") For Each myColumn As DataColumn In targetTable.Columns myBuilder.Append("<td align='left' valign='top'>") myBuilder.Append(myColumn.ColumnName) myBuilder.Append("</td>") Next myColumn myBuilder.Append("</tr>") 'Add the data rows. For Each myRow As DataRow In targetTable.Rows myBuilder.Append("<tr align='left' valign='top'>") For Each myColumn As DataColumn In targetTable.Columns myBuilder.Append("<td align='left' valign='top'>") myBuilder.Append(myRow(myColumn.ColumnName).ToString()) myBuilder.Append("</td>") Next myColumn myBuilder.Append("</tr>") Next myRow 'Close tags. myBuilder.Append("</table>") myBuilder.Append("</body>") myBuilder.Append("</html>") 'Get the string for return. myHtmlFile = myBuilder.ToString() Return myHtmlFile End Functionadeelalvi
Member
218 Points
58 Posts
Re: convert a DataTable to HTML
Oct 31, 2007 05:04 AM|LINK
easiet way of doing this , i think this can be helpfull in a senario of generating email with dynamic content (from db) as well.
vraj2k3
Member
84 Points
38 Posts
Re: convert a DataTable to HTML
Jun 14, 2010 07:26 AM|LINK
Yes I did,
I can give the code as a DLL, mail me if you need.
cheers...
convert datatable Html
VenkatRaj @ VRaj
Please mark this thread as "Answer", if it was useful to you.