2012-08-17 3 views
0

I는 다음과 같이하여 SqlDataReader의 값이있는 경우 :동적 HTML 테이블

| 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 | 

제가 하나의 열만 될 년 열 병합 등. 임의의 열이 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로 테이블을 출력 할 수있는 결과를 만들 수 있습니까?

+0

sqlDataReader를 생성하는 데이터베이스 쿼리를 변경하는 것이 더 간단 할 수 있습니다. 귀하의 질의를 게시하면 귀하를 보여줄 수 있습니다. – tgolisch

답변

0

각 레코드에 대해 새 테이블 행을 추가하여 결과 세트를 스핀합니다. 변수를 사용하여 모든 셀을 추가해야하는 경우 (일부는 rowspans 포함) 또는 모든 행에 표시된 셀만 추가 할 때를 추적하십시오. 여기에 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 
+0

NoAlias에 감사드립니다. 저는 직접 설정하겠습니다. – Pengan