合并datagrid中内容相同的单元格

下面就说说怎么实现的:

Sub SpanGrid()
        Dim i As Integer
        Dim j As Integer
        Dim intSpan As Integer,NowSpan As Integer = 0
        Dim strTemp As String
        For i = 0 To DGrid.Items.Count - 1
            intSpan = 1   

'得到第一列(颜色)、第一行单元格中的内容。这里得到是“红色Red”。(datagrid里用了模版列)
            strTemp = CType(DGrid.Items(i).Cells(0).Controls(1), System.Web.UI.WebControls.Label).Text

’循环判断。判断第一列中,和第一行相同的内容。相同做记号,intspan加一
            For j = i + 1 To DGrid.Items.Count - 1
                If String.Compare(strTemp, CType(DGrid.Items(j).Cells(1).Controls(1), System.Web.UI.WebControls.Label).Text) = 0 Then
                    intSpan += 1

'利用datagrid的rowspan属性。(设置控件中单元格跨越的行数为intspan)
                    DGrid.Items(i).Cells(0).RowSpan = intSpan

’把内容相同单元格隐藏
                    DGrid.Items(j).Cells(0).Visible = False
                Else
                    Exit For
                End If
            Next
            'i = j - 1 我原来的

            NowSpan += intSpan  '修改后的可以节省循环次数.
            i = NowSpan - 1
        Next
    End Sub

=======

引用:

 Sub bindgrid()
        '把数据绑定到datagrid        
        ........
        SpanGrid()
End Sub

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页