2012-08-17 76 views
0

如果非要從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 | 

我想合併Year列,成爲只有一列。如果任何列爲null,那麼它將合併成一列,如果任何當前行和下一行都爲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 | 

如何創建一個HTML表格可輸出的結果如上在vb.net?

+0

更改生成sqlDataReader的數據庫查詢可能會更簡單。發佈您的查詢,我可以告訴你。 – tgolisch 2012-08-17 18:21:35

回答

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 2012-08-18 03:55:52