0

以下のように sqlDataReader からの値がある場合:

|  YEAR1 |   NAM1 |  CRDT1 | SEMER1 | YEAR2 |   NAM2 | CRDT2 | SEMER2 |
-----------------------------------------------------------------------
|      1 |  Name1 |      1 |      1 |     1 | Name10 |     1 |      2 |
|      1 |  Name2 |      4 |      1 |     1 |  Name5 |     4 |      2 |
|      1 |  Name3 |      2 |      1 |     1 |  Name6 |     3 |      2 |
|      1 |  Name4 |      7 |      1 |     1 |  Name7 |     6 |      2 |
| (null) | (null) | (null) | (null) |     1 |  Name8 |     1 |      2 |
| (null) | (null) | (null) | (null) |     1 |  Name9 |     1 |      2 |
|      2 | Name11 |      3 |      1 |     2 | Name14 |     2 |      2 |
|      2 | Name12 |      6 |      1 |     2 | Name15 |     1 |      2 |
|      2 | Name13 |      4 |      1 |     2 | Name16 |     1 |      2 |
| (null) | (null) | (null) | (null) |     2 | Name17 |     1 |      2 |
|      3 | Name18 |      5 |      1 |     3 | Name18 |     5 |      2 |
|      3 | Name19 |      1 |      1 |     3 | Name19 |     1 |      2 |
|      3 | Name20 |      1 |      1 |     3 | Name20 |     1 |      2 |

年列をマージして、1列だけにするのが好きです。いずれかの列が null の場合は 1 つの列にマージされ、現在の行と次の行が null の場合はマージされます。

私はこのように出力したい:

 |  YEAR1 |   NAM1 |  CRDT1 | SEMER1 | YEAR2 |   NAM2 | CRDT2 | SEMER2 |
    -----------------------------------------------------------------------
 |        |  Name1 |      1 |      1 |       | Name10 |     1 |      2 |
 |    1   |  Name2 |      4 |      1 |   1   |  Name5 |     4 |      2 |
 |        |  Name3 |      2 |      1 |       |  Name6 |     3 |      2 |
 |        |  Name4 |      7 |      1 |       |  Name7 |     6 |      2 |
 |              (null)               |       |  Name8 |     1 |      2 |
 |                                   |       |  Name9 |     1 |      2 |
 |        | Name11 |      3 |      1 |       | Name14 |     2 |      2 |
 |    2   | Name12 |      6 |      1 |    2  | Name15 |     1 |      2 |
 |        | Name13 |      4 |      1 |       | Name16 |     1 |      2 |
 |              (null)               |       | Name17 |     1 |      2 |
 |        | Name18 |      5 |      1 |       | Name18 |     5 |      2 |
 |    3   | Name19 |      1 |      1 |    3  | Name19 |     1 |      2 |
 |        | Name20 |      1 |      1 |       | Name20 |     1 |      2 |

vb.netで上記の結果を出力できるhtmlテーブルを作成するにはどうすればよいですか?

4

1 に答える 1

0

結果セットをスピンして、レコードごとに新しいテーブル行を追加します。変数を使用して、すべてのセル (行スパンのあるもの) またはすべての行に表示されるセルのみを追加する必要がある時期を追跡します。これがコードの 90% です。あなたは残りを理解できるはずです。

    'Convert DataReader into DataTable.
    Dim dtResults As New DataTable
    dtResults.Load(drResults)

    'You can define this in your .Aspx
    Dim tblStudents As New Table

    'These will help you with your table building logic.
    Dim intCurrentYear1 As Integer = 0
    Dim intRowSpan As Integer = 0
    Dim bolDetermineRowSpan = True

    For intRowCursor As Integer = 0 To (dtResults.Rows.Count - 1)

        If bolDetermineRowSpan Then

            'First get the current year (Nulls will be set to 0, but not displayed as such.)
            If dtResults.Rows(intRowCursor).Item("Year1") Is DBNull.Value Then

                intCurrentYear1 = 0

            Else

                intCurrentYear1 = CInt(dtResults.Rows(intRowCursor).Item("Year1"))

            End If

            If intCurrentYear1 > 0 Then

                'Get the total number of records with this year, so we know how many cells to merge.
                intRowSpan = (From d As DataRow In dtResults.Rows _
                              Where Not d.Item("Year1") Is DBNull.Value AndAlso _
                              CInt(d.Item("Year1")) = intCurrentYear1).Count()

            Else

                'Figure out how many null records until the next year, so we know how many to merge.
                Dim bolNextYear As Boolean = False
                Dim intTempCursor As Integer = intRowCursor + 1

                intRowSpan = 1

                Do Until bolNextYear = True OrElse intTempCursor = dtResults.Rows.Count

                    If Not dtResults.Rows(intTempCursor).Item("Year1") Is DBNull.Value Then

                        bolNextYear = True

                    End If

                    intTempCursor += 1
                    intRowSpan += 1

                Loop

            End If

        End If

        Dim tr As New TableRow

        If intCurrentYear1 > 0 Then

            If bolDetermineRowSpan = True Then

                'Add all cells to this Table Row, using RowSpan property to merge desired fields.
                Dim tdYear1 As New TableCell
                tdYear1.RowSpan = intRowSpan
                tdYear1.Text = intCurrentYear1
                tdYear1.VerticalAlign = VerticalAlign.Middle

                tr.Cells.Add(tdYear1)

                Dim tdName1 As New TableCell
                tdName1.Text = CStr(dtResults.Rows(intRowCursor).Item("NAM1"))

                tr.Cells.Add(tdName1)

                'Add the rest of your cells here (omitted).

                'Update this variable to keep sound logic.
                bolDetermineRowSpan = False

            Else

                'Do not add the Year1 Cell because of RowSpan/"Merging".

                Dim tdName1 As New TableCell
                tdName1.Text = CStr(dtResults.Rows(intRowCursor).Item("NAM1"))

                tr.Cells.Add(tdName1)

                'Do for rest of cells.

                'Update this variable to keep sound logic.
                bolDetermineRowSpan = False

            End If

        Else

            'Same logic as other half of this If Statement block, just doing more
            'cells with RowSpans. (These are the null records.)
            If bolDetermineRowSpan = True Then

                'Add all cells (with rowspans)

            Else

                'Add just cells that are displayed on every row.

            End If

        End If

        'Add the row to the table.
        tblStudents.Rows.Add(tr)

        'Do we need to reset our boolean flag to determine row span?
        If bolDetermineRowSpan = False AndAlso dtResults.Rows.Count < (intRowCursor + 1) AndAlso _
           Not dtResults.Rows(intRowCursor + 1).Item("Year1") Is DBNull.Value AndAlso _
               dtResults.Rows(intRowCursor + 1).Item("Year1") <> intCurrentYear1 Then

            bolDetermineRowSpan = True

        End If

    Next
于 2012-08-17T19:25:49.190 に答える