[vb.net] Merge DataGridView Cells / DataGridView 셀병합 > vb.net

본문 바로가기

vb.net

[Controls] [vb.net] Merge DataGridView Cells / DataGridView 셀병합

회원사진
하나를하더라도최선을
2021-05-02 11:34 4,618 0

본문



HMergedCell.vb
Imports System 
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Diagnostics
Imports System.Drawing.Drawing2D
 
 
Public Class HMergedCell
    Inherits DataGridViewTextBoxCell
 
    Private m_nLeftColumn As Integer = 0
    Private m_nRightColumn As Integer = 0
 
    Private m_nTopRow As Integer = 0
    Private m_nBottomRow As Integer = 0
    Public Property LeftColumn As Integer
        Get
            Return m_nLeftColumn
        End Get
        Set(ByVal value As Integer)
            m_nLeftColumn = value
        End Set
    End Property
 
    Public Property RightColumn As Integer
        Get
            Return m_nRightColumn
        End Get
        Set(ByVal value As Integer)
            m_nRightColumn = value
        End Set
    End Property
 
 
    Public Property TopRow As Integer
        Get
            Return m_nTopRow
        End Get
        Set(ByVal value As Integer)
            m_nTopRow = value
        End Set
    End Property
 
 
    Public Property BottomRow As Integer
        Get
            Return m_nBottomRow
        End Get
        Set(ByVal value As Integer)
            m_nBottomRow = value
        End Set
    End Property
    Protected Overrides Sub Paint(ByVal graphics As Graphics, ByVal clipBounds As Rectangle, ByVal cellBounds As Rectangle, ByVal rowIndex As Integer, ByVal cellState As DataGridViewElementStates, ByVal value As ObjectByVal formattedValue As ObjectByVal errorText As StringByVal cellStyle As DataGridViewCellStyle, ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, ByVal paintParts As DataGridViewPaintParts)
        Try
            Dim mergeindex As Integer = ColumnIndex - m_nLeftColumn
            Dim i As Integer
            Dim nWidth As Integer, nHeight As Integer
            Dim nWidthLeft As Integer, nHeightTop As Integer
            Dim strText As String
            Dim pen As Pen = New Pen(Brushes.Black)
            graphics.FillRectangle(New SolidBrush(Color.LightGray), cellBounds)
            If rowIndex = m_nBottomRow Then
                graphics.DrawLine(New Pen(New SolidBrush(Color.White)), cellBounds.Left, cellBounds.Bottom - 1, cellBounds.Right, cellBounds.Bottom - 1)
            End If
            If ColumnIndex = m_nRightColumn Then
                graphics.DrawLine(New Pen(New SolidBrush(Color.White)), cellBounds.Right - 1, cellBounds.Top, cellBounds.Right - 1, cellBounds.Bottom)
            End If
            Dim rectDest As RectangleF = RectangleF.Empty
            Dim sf As StringFormat = New StringFormat()
            sf.Alignment = StringAlignment.Center
            sf.LineAlignment = StringAlignment.Center
            sf.Trimming = StringTrimming.EllipsisCharacter
            nWidth = 0 : nHeight = 0
 
            For i = m_nLeftColumn To m_nRightColumn
                nWidth += Me.OwningRow.Cells(i).Size.Width
            Next
 
            For i = m_nTopRow To m_nBottomRow
                nHeight += Me.DataGridView.Rows(i).Cells(ColumnIndex).Size.Height
            Next
 
            nWidthLeft = 0 : nHeightTop = 0
 
            For i = m_nLeftColumn To ColumnIndex - 1
                nWidthLeft += Me.OwningRow.Cells(i).Size.Width
            Next
 
            For i = m_nTopRow To rowIndex - 1
                nHeightTop += Me.DataGridView.Rows(i).Cells(ColumnIndex).Size.Height
            Next
 
            strText = Me.OwningRow.Cells(m_nLeftColumn).Value.ToString()
            rectDest = New RectangleF(cellBounds.Left - nWidthLeft, cellBounds.Top - nHeightTop, nWidth, nHeight)
            graphics.DrawString(strText, New Font(Me.DataGridView.Font.Name, Me.DataGridView.Font.Size), Brushes.Black, rectDest, sf)
        Catch ex As Exception
            Trace.WriteLine(ex.ToString())
        End Try
    End Sub
End Class
 
'// Examle
        Dim DGV As DataGridView = DL.DataGridView1
        Dim sr As Integer, er As Integer, value As String
        Dim pCell As HMergedCell
        For i As Integer = 1 To DGV.Rows.Count - 1
            If IsDBNull(DGV.Rows(sr).Cells("Subject").Value) Then DGV.Rows(sr).Cells("Subject").Value = ""
            If IsDBNull(DGV.Rows(i).Cells("Subject").Value) Then DGV.Rows(i).Cells("Subject").Value = ""
            If DGV.Rows(sr).Cells("Subject").Value = DGV.Rows(i).Cells("Subject").Value Then
                er = i
            Else
                If sr < er Then
                    value = DGV.Rows(sr).Cells("Subject").Value
                    For n As Integer = sr To er
                        DGV.Rows(n).Cells("Subject"= New HMergedCell()
                        pCell = CType(DGV.Rows(n).Cells("Subject"), HMergedCell)
                        pCell.LeftColumn = 0
                        pCell.RightColumn = 0
                        pCell.TopRow = sr
                        pCell.BottomRow = er
                        pCell.Value = value
                    Next
                End If
                sr = i
            End If
        Next
        If sr > 0 And sr < er Then
            value = DGV.Rows(sr).Cells("Subject").Value
            For n As Integer = sr To er
                DGV.Rows(n).Cells("Subject"= New HMergedCell()
                pCell = CType(DGV.Rows(n).Cells("Subject"), HMergedCell)
                pCell.LeftColumn = 0
                pCell.RightColumn = 0
                pCell.TopRow = sr
                pCell.BottomRow = er
                pCell.Value = value
            Next
        End If

댓글목록0

등록된 댓글이 없습니다.
게시판 전체검색