Imports System.Drawing
Imports System.Windows.Forms.VisualStyles
Public Class CheckBoxColumn
Inherits Windows.Forms.DataGridViewCheckBoxColumn
Sub New()
Me.CellTemplate = New CheckBoxCell
End Sub
End Class
Public Class TextBoxColumn
Inherits Windows.Forms.DataGridViewTextBoxColumn
Sub New()
Me.CellTemplate = New TextBoxCell
End Sub
End Class
Public Class TextBoxCell
Inherits Windows.Forms.DataGridViewTextBoxCell
Public Overrides Function AdjustCellBorderStyle( _
ByVal dataGridViewAdvancedBorderStyleInput As Windows.Forms.DataGridViewAdvancedBorderStyle, _
ByVal dataGridViewAdvancedBorderStylePlaceHolder As Windows.Forms.DataGridViewAdvancedBorderStyle, _
ByVal singleVerticalBorderAdded As Boolean, _
ByVal singleHorizontalBorderAdded As Boolean, _
ByVal firstVisibleColumn As Boolean, _
ByVal firstVisibleRow As Boolean) As Windows.Forms.DataGridViewAdvancedBorderStyle
' Customize the top border of cells in the first row and the
' right border of cells in the first column. Use the input style
' for all other borders.
With dataGridViewAdvancedBorderStylePlaceHolder
If firstVisibleColumn Then
.Left = Windows.Forms.DataGridViewAdvancedCellBorderStyle.Single
Else
.Left = Windows.Forms.DataGridViewAdvancedCellBorderStyle.None
End If
If firstVisibleRow Then
.Top = Windows.Forms.DataGridViewAdvancedCellBorderStyle.Single
Else
.Top = Windows.Forms.DataGridViewAdvancedCellBorderStyle.None
End If
If Me.ColumnIndex = DataGridView.DisplayedColumnCount(False) - 1 Then
.Right = Windows.Forms.DataGridViewAdvancedCellBorderStyle.Single
Else
.Right = Windows.Forms.DataGridViewAdvancedCellBorderStyle.None
End If
If RowIndex = DataGridView.FirstDisplayedCell.RowIndex + DataGridView.DisplayedRowCount(False) - 1 Then
.Bottom = Windows.Forms.DataGridViewAdvancedCellBorderStyle.Single
Else
.Bottom = Windows.Forms.DataGridViewAdvancedCellBorderStyle.None
End If
' .Right = dataGridViewAdvancedBorderStyleInput.Right
' .Bottom = dataGridViewAdvancedBorderStyleInput.Bottom
End With
Return dataGridViewAdvancedBorderStylePlaceHolder
End Function
'Protected Overrides Sub Paint(graphics As Drawing.Graphics, clipBounds As Drawing.Rectangle, cellBounds As Drawing.Rectangle, rowIndex As Integer, elementState As Windows.Forms.DataGridViewElementStates, value As Object, formattedValue As Object, errorText As String, cellStyle As Windows.Forms.DataGridViewCellStyle, advancedBorderStyle As Windows.Forms.DataGridViewAdvancedBorderStyle, paintParts As Windows.Forms.DataGridViewPaintParts)
' MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
' ' Draw Background
' If (paintParts And Windows.Forms.DataGridViewPaintParts.SelectionBackground) = Windows.Forms.DataGridViewPaintParts.SelectionBackground Then
' If rowIndex = DataGridView.CurrentRow.Index AndAlso DataGridView.SelectionMode = Windows.Forms.DataGridViewSelectionMode.FullRowSelect Then
' Using cellBackground As New SolidBrush(cellStyle.SelectionBackColor)
' graphics.FillRectangle(cellBackground, cellBounds)
' End Using
' Else
' Using cellBackground As New SolidBrush(cellStyle.BackColor)
' graphics.FillRectangle(cellBackground, cellBounds)
' End Using
' End If
' End If
' ' Draw Borders
' If (paintParts And Windows.Forms.DataGridViewPaintParts.Border) = Windows.Forms.DataGridViewPaintParts.Border Then
' With advancedBorderStyle
' ' .All = Windows.Forms.DataGridViewAdvancedCellBorderStyle.None
' 'If rowIndex = DataGridView.FirstDisplayedCell.RowIndex Then
' ' .Top = Windows.Forms.DataGridViewAdvancedCellBorderStyle.Single
' 'End If
' If Me.ColumnIndex = DataGridView.FirstDisplayedCell.ColumnIndex Then
' .Left = Windows.Forms.DataGridViewAdvancedCellBorderStyle.Single
' Else
' .Left = Windows.Forms.DataGridViewAdvancedCellBorderStyle.None
' End If
' If Me.ColumnIndex = DataGridView.DisplayedColumnCount(False) - 1 Then
' .Right = Windows.Forms.DataGridViewAdvancedCellBorderStyle.Single
' Else
' .Right = Windows.Forms.DataGridViewAdvancedCellBorderStyle.None
' End If
' If cellBounds.X + cellBounds.Width = clipBounds.Width Then
' End If
' If rowIndex = DataGridView.FirstDisplayedCell.RowIndex + DataGridView.DisplayedRowCount(False) - 1 Then
' .Bottom = Windows.Forms.DataGridViewAdvancedCellBorderStyle.Single
' Else
' .Bottom = Windows.Forms.DataGridViewAdvancedCellBorderStyle.None
' End If
' End With
' PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle)
' End If
'End Sub
End Class
Public Class CheckBoxCell
'Public Overrides Function AdjustCellBorderStyle( _
' ByVal dataGridViewAdvancedBorderStyleInput As Windows.Forms.DataGridViewAdvancedBorderStyle, _
' ByVal dataGridViewAdvancedBorderStylePlaceHolder As Windows.Forms.DataGridViewAdvancedBorderStyle, _
' ByVal singleVerticalBorderAdded As Boolean, _
' ByVal singleHorizontalBorderAdded As Boolean, _
' ByVal firstVisibleColumn As Boolean, _
' ByVal firstVisibleRow As Boolean) As Windows.Forms.DataGridViewAdvancedBorderStyle
' ' Customize the top border of cells in the first row and the
' ' right border of cells in the first column. Use the input style
' ' for all other borders.
' If firstVisibleColumn Then
' dataGridViewAdvancedBorderStylePlaceHolder.Left = _
' Windows.Forms.DataGridViewAdvancedCellBorderStyle.Single
' Else
' dataGridViewAdvancedBorderStylePlaceHolder.Left = dataGridViewAdvancedBorderStyleInput.Left
' End If
' If firstVisibleRow Then
' dataGridViewAdvancedBorderStylePlaceHolder.Top = _
' Windows.Forms.DataGridViewAdvancedCellBorderStyle.Single
' Else
' dataGridViewAdvancedBorderStylePlaceHolder.Top = dataGridViewAdvancedBorderStyleInput.Top
' End If
' With dataGridViewAdvancedBorderStylePlaceHolder
' .Right = dataGridViewAdvancedBorderStyleInput.Right
' .Bottom = dataGridViewAdvancedBorderStyleInput.Bottom
' End With
' Return dataGridViewAdvancedBorderStylePlaceHolder
'End Function
Protected Overrides Function GetFormattedValue(value As Object, rowIndex As Integer, ByRef cellStyle As Windows.Forms.DataGridViewCellStyle, valueTypeConverter As ComponentModel.TypeConverter, formattedValueTypeConverter As ComponentModel.TypeConverter, context As Windows.Forms.DataGridViewDataErrorContexts) As Object
Return If(Me.Value Is Nothing OrElse Not Convert.ToBoolean(Me.Value), False, Convert.ToBoolean(Me.Value))
End Function
Protected Overrides Sub Paint(graphics As Drawing.Graphics, clipBounds As Drawing.Rectangle, cellBounds As Drawing.Rectangle, rowIndex As Integer, elementState As Windows.Forms.DataGridViewElementStates, value As Object, formattedValue As Object, errorText As String, cellStyle As Windows.Forms.DataGridViewCellStyle, advancedBorderStyle As Windows.Forms.DataGridViewAdvancedBorderStyle, paintParts As Windows.Forms.DataGridViewPaintParts)
MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
Dim x As Int32 = CInt(cellBounds.Width / 2 - 8)
Dim y As Int32 = CInt(cellBounds.Height / 2 - 8)
With graphics
' Draw Background
If (paintParts And Windows.Forms.DataGridViewPaintParts.SelectionBackground) = Windows.Forms.DataGridViewPaintParts.SelectionBackground Then
If rowIndex = DataGridView.CurrentRow.Index AndAlso DataGridView.SelectionMode = Windows.Forms.DataGridViewSelectionMode.FullRowSelect Then
Using cellBackground As New SolidBrush(cellStyle.SelectionBackColor)
graphics.FillRectangle(cellBackground, cellBounds)
End Using
Else
Using cellBackground As New SolidBrush(cellStyle.BackColor)
graphics.FillRectangle(cellBackground, cellBounds)
End Using
End If
End If
' Draw Borders
If (paintParts And Windows.Forms.DataGridViewPaintParts.Border) = Windows.Forms.DataGridViewPaintParts.Border Then
With advancedBorderStyle
' .All = Windows.Forms.DataGridViewAdvancedCellBorderStyle.None
'If rowIndex = DataGridView.FirstDisplayedCell.RowIndex Then
' .Top = Windows.Forms.DataGridViewAdvancedCellBorderStyle.Single
'End If
If Me.ColumnIndex = DataGridView.FirstDisplayedCell.ColumnIndex Then
.Left = Windows.Forms.DataGridViewAdvancedCellBorderStyle.Single
Else
.Left = Windows.Forms.DataGridViewAdvancedCellBorderStyle.None
End If
If Me.ColumnIndex = DataGridView.DisplayedColumnCount(False) - 1 Then
.Right = Windows.Forms.DataGridViewAdvancedCellBorderStyle.Single
Else
.Right = Windows.Forms.DataGridViewAdvancedCellBorderStyle.None
End If
If cellBounds.X + cellBounds.Width = clipBounds.Width Then
End If
'If rowIndex = DataGridView.FirstDisplayedCell.RowIndex + DataGridView.DisplayedRowCount(False) - 1 Then
' .Bottom = Windows.Forms.DataGridViewAdvancedCellBorderStyle.Single
'End If
If cellBounds.Y + 1 <> clipBounds.Height Then
End If
End With
PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle)
End If
' CheckBox
Using cellBackground As New SolidBrush(cellStyle.BackColor)
graphics.FillRectangle(cellBackground, x + cellBounds.X, y + cellBounds.Y, 15, 15)
End Using
checkBrush.Color = Me.DataGridView.RowsDefaultCellStyle.ForeColor
checkPen.Color = Me.DataGridView.GridColor
.DrawRectangle(checkPen, x + cellBounds.X, y + cellBounds.Y, 15, 15)
' Check State
If CBool(EditedFormattedValue) = True Then
.DrawString("✓", checkFont, checkBrush, New Point(x + cellBounds.X, y + cellBounds.Y))
End If
End With
End Sub
Private checkFont As New Font("Segoe UI", 9, FontStyle.Bold)
Private checkBrush As New SolidBrush(Color.Black)
Private checkPen As New Pen(Color.Black, 1)
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
End Class
Public Class DataGridViewCustomRow
Inherits Windows.Forms.DataGridViewRow
Public Overrides Function AdjustRowHeaderBorderStyle( _
ByVal dataGridViewAdvancedBorderStyleInput As Windows.Forms.DataGridViewAdvancedBorderStyle, _
ByVal dataGridViewAdvancedBorderStylePlaceHolder As Windows.Forms.DataGridViewAdvancedBorderStyle, _
ByVal singleVerticalBorderAdded As Boolean, _
ByVal singleHorizontalBorderAdded As Boolean, _
ByVal isFirstDisplayedRow As Boolean, _
ByVal isLastDisplayedRow As Boolean) As Windows.Forms.DataGridViewAdvancedBorderStyle
' Customize the top border of the first row header and the
' right border of all the row headers. Use the input style for
' all other borders.
If isFirstDisplayedRow Then
dataGridViewAdvancedBorderStylePlaceHolder.Top = _
Windows.Forms.DataGridViewAdvancedCellBorderStyle.Single
Else
dataGridViewAdvancedBorderStylePlaceHolder.Top = _
Windows.Forms.DataGridViewAdvancedCellBorderStyle.None
End If
With dataGridViewAdvancedBorderStylePlaceHolder
.Right = Windows.Forms.DataGridViewAdvancedCellBorderStyle.Single
.Left = dataGridViewAdvancedBorderStyleInput.Left
.Bottom = dataGridViewAdvancedBorderStyleInput.Bottom
End With
Return dataGridViewAdvancedBorderStylePlaceHolder
End Function
End Class