在vb.net中创建一个动态表

时间:2016-02-11 14:26:28

标签: asp.net vb.net

我正在尝试创建一个动态表格以在Web表单内容占位符中显示。 我用下面的简单测试

Private Function createmyTab() As Table
    Dim tbl As New Table
    For i As Integer = 10 To 0 Step -1
        Dim tr As TableRow = New TableRow()
        For j As Integer = 5 To 0 Step -1
            'i = i + 1
            Dim tc As TableCell = New TableCell()
            Dim txtBox As TextBox = New TextBox()
            txtBox.Text = "RowNo:" & i & " " & "ColumnNo:" & " " & j
            ' Add the control to the TableCell
            tc.Controls.Add(txtBox)
            ' Add the TableCell to the TableRow
            tr.Cells.Add(tc)
            Exit For
        Next j
        ' Add the TableRow to the Table
        tbl.Rows.Add(tr)
    Next i
    Return tbl
End Function  

结果如下所示

Row 10 Col 5  Row 10 Col 4 Row 10 Col 3 Row 10 Col 2 Row 10 Col 1
Row 9  col 5  Row 9  col 4 Row 9 Col 3 Row 9 Col 2 Row 9 Col 1
Row 8  col 5  Row 8  col 4 Row 8 Col 3 Row 8 Col 2 Row 8 Col 1
Row 7  col 5  Row 7  col 4 Row 7 Col 3 Row 7 Col 2 Row 7 Col 1
Row 6  col 5  Row 6  col 4 Row 6 Col 3 Row 6 Col 2 Row 6 Col 1

我想要下面的内容

Row 10 col 5  Row 9 col 5  Row 8 col 5  Row 7 Row 6
Row 10 col 4  Row 9 col 4  Row 8 col 4  .     .
Row 10 col 3  Row 9 col 3  Row 8 col 3  .     ..
Row 10 col 2  Row 9 col 2  Row 8 col 2  .     ...
Row 10 col 1  Row 9 col 1  Row 8 col 1  .     ....

实现此目的的实际目的如下Image1 (不是按钮,而是文本框)

我以正确的格式获得数字,但AA018 AA016 AA014 ....应该像

AA018    AA017 AB018    AB017 AC018
AA016    AA015 AB016    AB015 AC016
AA014    AA013 AB014    AB013 AC014

有人可以帮帮我吗? 请参阅下面的图片,以明确Invalid Layout

1 个答案:

答案 0 :(得分:0)

您需要提前创建细胞,然后填充它们以获得您想要的垂直方面。

Private Function createmyTab() As Table
    Dim tbl As New Table
    For i As Integer = 10 To 0 Step -1
         Dim tr as TableRow = New TableRow()
         For j as Integer =5 to 0 Step -1
              Dim tc as TableCell = New TableCell()
              tr.Cells.Add(tc);
         Next j
    tbl.Rows.Add(tr)
    Next i

    For i as Integer = 10 to 0 Step -1
        For j As Integer = 5 To 0 Step -1
            Dim txtBox As TextBox = New TextBox()
            txtBox.Text = "RowNo:" & i & " " & "ColumnNo:" & " " & j
            ' Add the control to the TableCell
            TableCell tc = tbl.Rows(i).Cells(i)
            tc.Controls.Add(txtBox)
            Exit For
        Next j
    Next i
    Return tbl
End Function

另一种方法是循环UP而不是DOWN,反转当前循环计数器,并反转代码中显示的信息......如下所示:

Private Function createmyTab() As Table
    Dim tbl As New Table
    For i As Integer = 0 To 5 Step 1
        Dim tr As TableRow = New TableRow()
        For j As Integer = 0 To 10 Step 1
            'i = i + 1
            Dim tc As TableCell = New TableCell()
            Dim txtBox As TextBox = New TextBox()
            txtBox.Text = "RowNo:" & j & " " & "ColumnNo:" & " " & i
            ' Add the control to the TableCell
            tc.Controls.Add(txtBox)
            ' Add the TableCell to the TableRow
            tr.Cells.Add(tc)
            Exit For
        Next j
        ' Add the TableRow to the Table
        tbl.Rows.Add(tr)
    Next i
    Return tbl
End Function

很抱歉,如果此代码示例略微关闭或无法编译。我更像是一个C#人。