Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET: Shortest way to render DataTable to a string (HTML)?

What is the shortest way to convert a DataTable into a string (formatted in HTML)?

Programmatically binding to a UI control and rendering to an ASP.NET page is not acceptable. Can't depend on the ASP.NET page lifecycle.

The purpose of this shouldn't matter, but to satisfy curiosity: This is for logging/debugging/dump purposes in algorithms that do a lot of DataTable processing.

Thanks!

like image 580
Matias Nino Avatar asked Jun 19 '09 16:06

Matias Nino


2 Answers

You can use the ASP.net controls such as GridView, DataGrid and point them render into StringBuilder using StringWriter, No need to use ASP.net page for this, this is a simple example in Console

class Program
{
    static void Main(string[] args)
    {
        IList<Person> persons = new List<Person>()
            {
               new Person{Id = 1, Name="Test Name 1"},
               new Person{Id = 2, Name="Test Name 2"}
            };

        GridView gridView = new GridView();
        StringBuilder result =  new StringBuilder();
        StringWriter writer = new StringWriter(result);
        HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
        gridView.DataSource = persons;
        gridView.DataBind();

        gridView.RenderControl(htmlWriter);

        Console.WriteLine(result);
    }


}


class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}
like image 198
bashmohandes Avatar answered Nov 11 '22 17:11

bashmohandes


I use this function through my application. It's pretty straightforward

  static public string ConvertDataTableToHTMLString(System.Data.DataTable dt, string filter, string sort, string fontsize, string border, bool headers, bool useCaptionForHeaders)
        {

            StringBuilder sb = new StringBuilder();
            sb.Append("<table border='" + border + "'b>");
            if (headers)
            {
                //write column headings
                sb.Append("<tr>");
                foreach (System.Data.DataColumn dc in dt.Columns)
                {
                    if (useCaptionForHeaders)
                        sb.Append("<td><b><font face=Arial size=2>" + dc.Caption + "</font></b></td>");
                    else
                        sb.Append("<td><b><font face=Arial size=2>" + dc.ColumnName + "</font></b></td>");
                }
                sb.Append("</tr>");
            }

            //write table data
            foreach (System.Data.DataRow dr in dt.Select(filter,sort))
            {
                sb.Append("<tr>");
                foreach (System.Data.DataColumn dc in dt.Columns)
                {
                    sb.Append("<td><font face=Arial size=" + fontsize + ">" + dr[dc].ToString() + "</font></td>");
                }
                sb.Append("</tr>");
            }
            sb.Append("</table>");

            return sb.ToString();
        }
like image 40
Cody C Avatar answered Nov 11 '22 17:11

Cody C