Logo UGIdotNET

Creare tabelle html mediante un DataSet

Potrebbe tornare utile rappresentare la struttura e il contenuto di un intero DataSet in formato html, con questo metodo, che come parametro accetta un DataSet, si potranno visualizzare tante tabelle per quante ne sono inserite nel DataSet, ovviamente questo metodo può essere raffinato in mille modi, per esempio passare un ulteriore parametro che permetta di selezionare una determinata tabella o altro ancora, comunque il parser è questo:

 

// Costanti che contengono i relativi tag di formattazione tabella
const string tbo="<table cellpadding='0' cellspacing='0' border='1'>";
const string tbc="</table>";
const string tro="<tr>";
const string trc="</tr>";
const string tdo="<td>";
const string tdc="</td>";

string html="";

// Metodo
private string getHtml(DataSet ds)
{
if(ds.Tables.Count!=0)
{
for(int t=0; t<ds.Tables.Count; t++)
{
html+= tbo+tro+tdo+ds.Tables[t].TableName+tdc+trc+tbc;

if(ds.Tables[t].Columns.Count!=0)
{
html+=tbo+tro;
for(int c=0; c<ds.Tables[t].Columns.Count;c++)
{
html+=tdo+ds.Tables[t].Columns[c].ColumnName+tdc;
}
html+=trc;

if(ds.Tables[t].Rows.Count!=0)
{
foreach(DataRow r in ds.Tables[t].Rows)
{
html+=tro;
for(int rC=0; rC<ds.Tables[t].Columns.Count; rC++)
{
html+=tdo+r[rC].ToString()+tdc;
}
html+=trc;
}
}
html+=tbc;
}
}
}
return html;
}

Autore: Giuseppe Falcone
Data: 24 dicembre 2002
Ultimo aggiornamento: 23 luglio 2004
Categorie: 

© 2001 User Group Italiano UGIdotNET. Tutti i diritti riservati. Note legali. - Partita IVA 01927050185