使用Vb.net

时间:2016-09-27 02:18:57

标签: javascript html css asp.net vb.net

我的表当前布局是这样的:

<tr>
   <td> Field1 </td>
   <td> Field2 </td>
   <td> Field3 </td>
   <td> Field4 </td>
   <td >Field5 </td>
</tr>

<tr>
   <td> anothefield1 </td>
   <td> anothefield2 </td>
   <td> anothefield3 </td>
   <td> anothefield1 </td>
   <td> anothefield1 </td>
</tr>

我想要实现的是在每列的每个td中动态添加一个css类。像这样:

<tr>
   <td class = '1'> Field1 </td>
   <td class = '2'> Field2 </td>
   <td class = '3'> Field3 </td>
   <td class = '4'> Field4 </td>
   <td class = '5'> Field5 </td>
</tr>

<tr>
   <td class = '1'> anothefield1 </td>
   <td class = '2'> anothefield2 </td>
   <td class = '3'> anothefield3 </td>
   <td class = '4'> anothefield4 </td>
   <td class = '5'> anothefield5 </td>
</tr>

等下一个tr。

我怎样才能实现这种布局?

这是我到目前为止的内容

Dim dv As New DataView(ds.Tables(0))
Dim dvfilter As DataTable = dv.ToTable(True, {"date"})
Dim dt2 As New DataTable
For Each dtrow As DataRow In dvfilter.Rows

dv.RowFilter = "Total_Load < Potential_Load"
dt2 = dv.ToTable(False, "Field1", "Field2", "Field3", "Field4", "Field5"})
Next

Dim builder As New StringBuilder
    builder.Append("<!DOCTYPE html><html>")
    builder.Append("<head>")
    builder.Append("</head>")
    builder.Append("<body>")
    builder.Append("<div class = 'email-section'>") 
    builder.Append("<table id = 'email' class='emai_table'>")
    builder.Append("<thead>")
    builder.Append("<tr>")
    builder.Append("<th>Field1</th>")
    builder.Append("<th>Field2</th>")
    builder.Append("<th>Field3</th>")
    builder.Append("<th>Field4</th>")
    builder.Append("<th>Field5</th>")
 For Each row As DataRow In dt2.Rows
    builder.Append("<tr>")
    For Each col As DataColumn In dt2.Columns
         builder.Append("<td>" & row(col).ToString + " " & "</td>")
    Next
         builder.Append("</tr>")
         builder.AppendLine()
Next
    builder.Append("</tbody>")
    builder.Append("</table>")
    builder.Append("</div>")
    builder.Append("</body>")
    builder.Append("</html>")

非常感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

在外部For循环中创建一个新计数器,然后在内部For循环内调出其值并递增。

For Each row As DataRow In dt2.Rows
    builder.Append("<tr>")
    Dim i as Integer
    i = 1
    For Each col As DataColumn In dt2.Columns
         builder.Append("<td class=""" & i & """>" & row(col).ToString + " " & "</td>")
         i = i + 1
    Next
         builder.Append("</tr>")
         builder.AppendLine()
Next

我的VB有点生疏,所以它可能不是100%正确的语法,但我认为你会得到这个要点。