Table Row的合併動作

Table Row的合併動作

日前看到有人提問,說圖一的呈現方式,能否變成圖二
  • 圖一

pic1

  • 圖二

pic2

我是有想到一個解法,就是利用主從關係來解,

但看到有人用另一個解法,覺得還不賴,就給他偷學了起來

資料表的設計及資料內容如下

tbdata

詳細的程式碼如下

我們在aspx只要放Button跟Table就好了


        <asp:Table ID="Table1" runat="server" BorderColor="#006666" BorderWidth="1px" CellPadding="0" CellSpacing="0" Width="100%">    
    </asp:Table>  
<asp:Button ID="Button1" runat="server" Text="Button" />
        <asp:Table ID="Table1" runat="server" BorderColor="#006666" BorderWidth="1px" CellPadding="0" CellSpacing="0" Width="100%">
    </asp:Table>

在aspx.vb撰寫相關的程式


Imports System.Text    
Imports System.Data.SqlClient    
   
Partial Class Default4    
    Inherits System.Web.UI.Page    
   
    Dim myConn As String = ".............." 'ConnectionString    
    Dim ds As DataSet    
   
    '設定搜尋條件的function    
    Function BB(ByVal i As Integer, ByVal dr As DataRow) As String   
        Dim F As String = ""   
        For j As Integer = 0 To i    
            F += ds.Tables(0).Columns(j).ColumnName & "='" & dr(j) & "' AND "   
        Next   
        Return Mid(F, 1, F.Length - 5)    
    End Function   
   
    '抓取是否有重複資料的function    
    Function CC(ByVal i As Integer, ByVal dr As DataRow, ByVal tmpA() As String) As Boolean   
        Dim thesame As Boolean = True   
        For j As Integer = 0 To i    
            If dr(j) <> tmpA(j) Then thesame = False   
        Next   
        Return thesame    
    End Function   
   
    '合併Table    
    Sub CombineTable()    
        '利用DataSet來比對    
        ds = New DataSet    
   
        Dim dap As New SqlDataAdapter("SELECT * FROM testtable ORDER BY test1, test2, test3, test4, test5, test6", myConn)    
   
        dap.Fill(ds)    
   
        Dim trA(ds.Tables(0).Rows.Count) As String '紀錄上一筆資料    
        Dim Rspan As Integer = 0    
   
        For Each dr As DataRow In ds.Tables(0).Rows    
            Dim tr As New TableRow    
   
            For i As Integer = 0 To ds.Tables(0).Columns.Count - 1    
                Dim tc As New TableCell    
                tc.BorderWidth = Unit.Parse("1px")    
                Dim Ins As Boolean = True   
                Rspan = 0    
   
                If Not CC(i, dr, trA) Then '判斷資料有無重複    
                    Dim Exp As String = Me.BB(i, dr)    
                    Rspan = ds.Tables(0).Select(Exp).Length '抓取該rowSpan的數量    
                Else   
                    Ins = False   
                End If   
   
                tc.Text = dr(i)    
                tc.RowSpan = Rspan    
                If Ins Then tr.Cells.Add(tc) '沒重複則把Cell加入    
            Next   
   
            Table1.Rows.Add(tr) '把資料加到Table1去    
   
            '紀錄目前dr    
            For i As Integer = 0 To ds.Tables(0).Columns.Count - 1    
                trA(i) = dr(i)    
            Next   
        Next   
    End Sub   
   
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click    
        CombineTable()    
    End Sub   
   
End Class  
Imports System.Data
Imports System.Text
Imports System.Data.SqlClient

Partial Class Default4
    Inherits System.Web.UI.Page

    Dim myConn As String = ".............." 'ConnectionString
    Dim ds As DataSet

    '設定搜尋條件的function
    Function BB(ByVal i As Integer, ByVal dr As DataRow) As String
        Dim F As String = ""
        For j As Integer = 0 To i
            F += ds.Tables(0).Columns(j).ColumnName & "='" & dr(j) & "' AND "
        Next
        Return Mid(F, 1, F.Length - 5)
    End Function

    '抓取是否有重複資料的function
    Function CC(ByVal i As Integer, ByVal dr As DataRow, ByVal tmpA() As String) As Boolean
        Dim thesame As Boolean = True
        For j As Integer = 0 To i
            If dr(j) <> tmpA(j) Then thesame = False
        Next
        Return thesame
    End Function

    '合併Table
    Sub CombineTable()
        '利用DataSet來比對
        ds = New DataSet

        Dim dap As New SqlDataAdapter("SELECT * FROM testtable ORDER BY test1, test2, test3, test4, test5, test6", myConn)

        dap.Fill(ds)

        Dim trA(ds.Tables(0).Rows.Count) As String '紀錄上一筆資料
        Dim Rspan As Integer = 0

        For Each dr As DataRow In ds.Tables(0).Rows
            Dim tr As New TableRow

            For i As Integer = 0 To ds.Tables(0).Columns.Count - 1
                Dim tc As New TableCell
                tc.BorderWidth = Unit.Parse("1px")
                Dim Ins As Boolean = True
                Rspan = 0

                If Not CC(i, dr, trA) Then '判斷資料有無重複
                    Dim Exp As String = Me.BB(i, dr)
                    Rspan = ds.Tables(0).Select(Exp).Length '抓取該rowSpan的數量
                Else
                    Ins = False
                End If

                tc.Text = dr(i)
                tc.RowSpan = Rspan
                If Ins Then tr.Cells.Add(tc) '沒重複則把Cell加入
            Next

            Table1.Rows.Add(tr) '把資料加到Table1去

            '紀錄目前dr
            For i As Integer = 0 To ds.Tables(0).Columns.Count - 1
                trA(i) = dr(i)
            Next
        Next
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        CombineTable()
    End Sub

End Class

如此之後,就能達到圖二的功能了