首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

怎么用循环做出有多列是多行的table

2013-03-14 
如何用循环做出有多列是多行的table遇到一个问题,从数据库查出数据以后要循环的把一个有多列都是多行的表

如何用循环做出有多列是多行的table
遇到一个问题,从数据库查出数据以后要循环的把一个有多列都是多行的表格画出来


    <table cellspacing="0" cellpadding="0" class="table_border" width="100%" style='text-align: center;'>
    <head>
    <style type="text/css">
        table
        {
            border-collapse: collapse;
            border: none;
            width: 200px;
        }
        td
        {
            border: solid #000 1px;
        }
    </style>

    </head>
        <tr height='32' style='text-align: center; background: #99ccff; font-weight: bolder;'>
            <td colspan="11" style="font-size: 15pt;">
                测试
            </td>
        </tr>
        <tr height='32' style='text-align: center; background: silver; font-weight: bolder;'>
            <td style='text-align: center;' width='80px'>
                <nobr>所属区域</nobr>
            </td>
            <td style='text-align: right;'>
                <nobr>所属公司</nobr>
            </td>
            <td style='text-align: right;'>
                <nobr>所属项目</nobr>
            </td>
        </tr>
        <tr style='text-align: center; background: white; font-weight: bolder;' height='32'
            height='28'>
            <td rowspan='3'>
                北方区
            </td>
            <td rowspan='2'>
                大连公司
            </td>
            <td>
                A项目


            </td>
        </tr>
        <tr>
            <td>B项目</td>
        </tr>
        <tr>
        <td>沈阳公司</td>
        <td>C项目</td>
        </tr>
    </table>


效果如下图
区域公司项目
北方区北京公司A
B
天津公司C
华南区A公司E
F
G
C公司H









[解决办法]
数据库得到ds然后遍历
// 可以根据tb 和td进行css设置
DataSet ds = new DataSet();
string StarHead = "<table class='tb'><tr>";
string EndHead = "</table>";
string Head = "";
// 设置表头信息
for(int i = 0; i < ds.Tables[0].Columns.Count;i++)
{
Head += "<td class='td"+ i +"'>";
Head += ds.Tables[0].Columns[i].ColumnName;
Head + "</td>";
}
Head = Head + "</tr>";
StarHead = StarHead + Head;
string Body = "";
// 设置数据信息
for(int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
Body = Body + "<tr>";
for(int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
Body = Body + "<td>";
Body = Body + ds.Tables[0].Rows[i][j].ToString();
Body = Body + "</td>";
}
Body = Body + "</tr>";
}
// 生成的表格html 
string html = StarHead + Body + EndHead;

热点排行