• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 305
  • Last Modified:

looping thru gridview not acting as expected

In vb.net/asp.net 2.0 I build this table from a gridview named gv:
(Code is simplified from actual)
Dim table1 As Table
Dim table2 As Table
 For Each grdItem As GridViewRow In gv.Rows
                        Dim row1 As New TableRow()
                        Dim cell1 As TableCell
                        cell1 = tbBPAddCell(row1, grdItem.Cells(0).Text, "center", "Style1", 4)
                        cell1 = tbBPAddCell(row1, grdItem.Cells(1).Text, "center", "Style1", 7, 2)

                        Dim row2 As New TableRow()
                        Dim cell2 As TableCell
                        cell2= tbBPAddCell(row2, grdItem.Cells(2).Text, "center", "Style1", 4)
                        cell2= tbBPAddCell(row2, grdItem.Cells(3).Text, "center", "Style1", 7, 2)

The problem lies in when the table built the first table is displayed as many times as there are rows and then the second table is displayed as many times as there are rows. Like this:

But! My goal is to display one table after another like this:

If the code worked like I thought it did I should get my desired results. I can't get it to work correctly with two separate tables. How can I loop thru this gridview to have my tables display one after another??? Please provide an example.
If it helps, here's the function called in the code snippit:
Public Function tbBPAddCell(ByVal row As TableRow, ByVal Contents As String, ByVal Align As String, ByVal CellType As String, ByVal ColSpan As Integer, Optional ByVal RSpan As Integer = 1, Optional ByVal Width As Integer = 0) As TableCell
        tbBPAddCell = New TableCell()
        tbBPAddCell.Font.Size = 9
        tbBPAddCell.Wrap = False
        tbBPAddCell.BorderColor = Drawing.Color.Black
        If CellType = "Style1" Then
            tbBPAddCell.Font.Size = 12
            tbBPAddCell.Wrap = True
            tbBPAddCell.ForeColor = Drawing.Color.Black
            tbBPAddCell.BorderStyle = BorderStyle.Solid
            tbBPAddCell.BorderColor = Drawing.Color.Black
            tbBPAddCell.BorderWidth = Unit.Pixel(1)
        End If
        tbBPAddCell.ColumnSpan = ColSpan
        tbBPAddCell.RowSpan = RSpan
        If Align = "left" Then
            tbBPAddCell.HorizontalAlign = HorizontalAlign.Left
        ElseIf Align = "center" Then
            tbBPAddCell.HorizontalAlign = HorizontalAlign.Center
            tbBPAddCell.HorizontalAlign = HorizontalAlign.Right
        End If
        tbBPAddCell.Text = Contents
    End Function

Open in new window

  • 2
1 Solution
I think a single table would suffice for you.

Instead of adding to two different tables table1 and table2. just keep on adding to table1

techpr0Author Commented:
Thanks for the feedback informaniac. That worked in testing but the two "real" tables are totally different and extremely long and being able to do two separate tables would save me a ton of work. That's the goal of this post.
techpr0Author Commented:
Nevermind, it was obvious. If you add to the same table (by name) it's going to append to the table of the same name the next time it loops. I had to reinstanciate the table name each time by putting the table declaration within the for each loop.

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now