日期:2014-05-17 浏览次数:20453 次
//javascript
//json={{columns:[A,B]},{data:[['a1','b1'],['a2','b2']]}};//datatable 转换的json示例
//假设页面引用了jQuery
$(function(){
var tab=$("<table/>");
var row= tab[0].insertRow(-1);
$(json.columns).each(function(){
var cell=row.insertCell(-1);
$(cell).html(this);
})
//以上代码会生成一个tr行,该行显示表头。其他自己写
})
//C#
//dataTable 输入的DataTable对象
System.Web.UI.HTMLControl.HTMLGer... hgc=new System.Web.UI.HTMLControl.HTMLGer...("div");//Ger...忘记怎么拼了
string th="";
foreach(Column cn in dataTable.Columns)
{
th+=string.format("<td>{0}</td>",cn.Name);
}
hgc.innerHTML=string.format("<table><tr>{0}</tr><table>",th);
Controls.Add(hgc);
//同样只有表头部分
CreateTable(37,4);
public string CreateTable(int cellCount, int columnCount)
{
StringBuilder resultSb = new StringBuilder();
resultSb.Append("<table border='1' width='300' height='300'>");
int trCount = cellCount / columnCount;
int trMod = cellCount % columnCount;
for (int i = 0; i < trCount; i++)
{
resultSb.Append("<tr>");
for (int j = 0; j < columnCount; j++)
{
resultSb.Append("<td></td>");
}
resultSb.Append("</tr>");
}
if (trMod > 0)
{
resultSb.Append("<tr>");
for (int k = 0; k < trMod; k++)
{
resultSb.Append("<td></td>");
}
resultSb.Append("<td colspan=\"" + (columnCount - trMod).ToString() + "\"></td>");
resultSb.Append("</tr>");
}
resultSb.Append("</table>");
return resultSb.ToString();
}