代码改变世界

我最近写的DataGrid合并/删除相同列通用函数,跟大家分享

2004-05-20 11:30  灵感之源  阅读(2429)  评论(2编辑  收藏  举报

鉴于相当多页面都要做合并列,我最近写了DataGrid合并/删除相同列通用函数,跟大家分享。
    Public Shared Sub DBGridSpanCell(ByRef dgdData As DataGrid, _
                                     ByVal intColumnItem As Integer, _
                                     ByVal intColumnStart As Integer, _
                                     ByVal intColumnEnd As Integer, _
                                     Optional ByVal intSpan As Integer = 2)
        '名称:DBGridSpanCell
        '功能:合并/删除相同的项目名
        '作者:dzt
        '创建:2004/1/1
        '修改:2004/1/1
        '参数:dgdData:DataGrid对象
        '      intColumnItem:要合并列
        '      intColumnStart:合并起始列
        '      intColumnEnd:合并结束列
        '      intSpan:跨行数
        '返回:成功,True,反之,False
        '说明:

        Dim i As Integer
        Dim o_intCols As Integer
        Dim iSpan As Integer = intSpan '跨行数
        Dim iRow As Integer = 0 '开始的跨行
        Dim sItem As String
        Dim sColumnText As String
        Dim ColumnType As Type
        Dim ctl As System.Web.UI.Control

        With dgdData.Items
            For i = 0 To .Count - 1
                With .Item(i)
                    ColumnType = dgdData.Columns(intColumnItem).GetType
                    '模板列,label
                    If ColumnType Is GetType(TemplateColumn) Then
                        For Each ctl In dgdData.Items(i).Cells(intColumnItem).Controls
                            If ctl.GetType Is GetType(Label) Then
                                sColumnText = CType(ctl, Label).Text
                                Exit For
                            End If
                        Next
                    Else
                        sColumnText = .Cells(intColumnItem).Text
                    End If

                    ' If .Cells(intColumnItem).Text = sItem Then
                    If sColumnText = sItem Then
                        For o_intCols = intColumnStart To intColumnEnd
                            dgdData.Items(iRow).Cells(o_intCols).RowSpan = iSpan
                            .Cells(o_intCols).Visible = False
                        Next

                        iSpan += 1
                    Else
                        iRow = i
                        iSpan = intSpan
                        'sItem = .Cells(intColumnItem).Text
                        sItem = sColumnText
                    End If
                End With
            Next
        End With
    End Sub