cara menggabungkan cell secara vertikal
CATATAN : If e.ColumnIndex = 2 adalah kolom *satuan*
Imports System.Data.OleDb
Public Class mergecell
Dim conn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Sub koneksidb()
conn = New OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=DBACCESS2003.mdb")
conn.Open()
End Sub
Private Sub mergecell_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call koneksidb()
dgv.Columns.Clear()
da = New OleDbDataAdapter("select * from tblbarang", conn)
ds = New DataSet
da.Fill(ds)
dgv.DataSource = ds.Tables(0)
End Sub
Private Sub DataGridView1_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles dgv.CellPainting
If e.ColumnIndex = 2 AndAlso e.RowIndex <> -1 Then
Using gridBrush As Brush = New SolidBrush(Me.dgv.GridColor), backColorBrush As Brush = New SolidBrush(e.CellStyle.BackColor)
Using gridLinePen As Pen = New Pen(gridBrush)
' Clear cell
e.Graphics.FillRectangle(backColorBrush, e.CellBounds)
' Draw line (bottom border and right border of current cell)
'If next row cell has different content, only draw bottom border line of current cell
If e.RowIndex < dgv.Rows.Count - 2 AndAlso dgv.Rows(e.RowIndex + 1).Cells(e.ColumnIndex).Value.ToString() <> e.Value.ToString() Then
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
End If
' Draw right border line of current cell
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom)
' draw/fill content in current cell, and fill only one cell of multiple same cells
If Not e.Value Is Nothing Then
If e.RowIndex > 0 AndAlso dgv.Rows(e.RowIndex - 1).Cells(e.ColumnIndex).Value.ToString() = e.Value.ToString() Then
Else
e.Graphics.DrawString(CType(e.Value, String), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault)
End If
End If
e.Handled = True
End Using
End Using
End If
End Sub
End Class