数据表到HTML表格 [英] Datatable to html Table
问题描述
我有问题,也许这里有人不介意帮助我。我已经可以说3数据表,他们每个人都有以下几列:
I have question, that maybe someone here wouldn't mind to help me with. I have lets say 3 datatables, each one of them has the following columns:
大小,数量,金额,期限
size, quantity, amount, duration
数据表和值的名称
LivingRoom
================
1
1
1
1
2
2
2
2
BathRoom
================
3
3
3
3
4
4
4
4
BedRoom
=================
5
5
5
5
6
6
6
6
现在我想建立一个HTML发票是我可以遍历所有的数据表和输出以下HTML输出,非常基本的:
Now i am trying to build an html invoice to were i can loop through all the datatables and output the following html output, very basic:
<table>
<tr>
<td>Area</td>
</tr>
<tr>
<td>Living Room</td>
</tr>
<tr>
<td>Size</td>
<td>Quantity</td>
<td>Amount</td>
<td>Duration</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>Area</td>
</tr>
<tr>
<td>Bathroom</td>
</tr>
<tr>
<td>Size</td>
<td>Quantity</td>
<td>Amount</td>
<td>Duration</td>
</tr>
<tr>
<td>3</td>
<td>3</td>
<td>3</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>4</td>
<td>4</td>
<td>4</td>
</tr>
<tr>
<td>Area</td>
</tr>
<tr>
<td>Bedroom</td>
</tr>
<tr>
<td>Size</td>
<td>Quantity</td>
<td>Amount</td>
<td>Duration</td>
</tr>
<tr>
<td>5</td>
<td>5</td>
<td>5</td>
<td>5</td>
</tr>
<tr>
<td>6</td>
<td>6</td>
<td>6</td>
<td>6</td>
</tr>
</table>
所以pretty大部分地区将有DataTable的名称,然后在每个区域循环使用该格式特定的数据表和输出datat。我想不通的循环逻辑或如何做到这一点,我已经打破了我的头就在这最后几天。也许我只是想着它在错误的方式,但我真的可以使用一些帮助在此。
So pretty much the area would have the name of the datatable, and then under each area loop that specific datatable and output the datat in that format. I can't figure out the looping logic or how to do this, i've been breaking my head for the last few days on this. maybe i'm just thinking about it in the wrong way but i could really use some help on this.
推荐答案
使用此功能:
public static string ConvertDataTableToHTML(DataTable dt)
{
string html = "<table>";
//add header row
html += "<tr>";
for(int i=0;i<dt.Columns.Count;i++)
html+="<td>"+dt.Columns[i].ColumnName+"</td>";
html += "</tr>";
//add rows
for (int i = 0; i < dt.Rows.Count; i++)
{
html += "<tr>";
for (int j = 0; j< dt.Columns.Count; j++)
html += "<td>" + dt.Rows[i][j].ToString() + "</td>";
html += "</tr>";
}
html += "</table>";
return html;
}
这篇关于数据表到HTML表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!