有时,我们要把一列中内容相同的单元格合并起来。如下图:

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

合并后的效果图:

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

下面就说说怎么实现的:

Sub SpanGrid()
        Dim i As Integer
        Dim j As Integer
        Dim intSpan As Integer
        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
        Next
    End Sub

=======

引用:

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

相关文章:

  • 2021-04-19
  • 2021-11-14
  • 2021-10-13
  • 2022-12-23
  • 2021-12-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-20
  • 2022-01-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-13
相关资源
相似解决方案