Saturday, January 18, 2014

Convert datatable to html table in c#

using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public static string Datatable2HTMLtable(DataTable dt)
{
       try
       {
              GridView dg = new GridView();
              dg.AutoGenerateColumns = true;
              dg.DataSource = dt;
              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);
              return sb.ToString();
       }
       catch (Exception ex)
       {
              return "Datatable2HTMLtable ERROR: " + ex.Message;
       }
}

No comments:

Post a Comment

Opps Part 1 : Abstraction

  Abstraction in C# is a fundamental concept of object-oriented programming (OOP) that allows developers t...