【DataGridViewCustomCheckBoxHeaderColumn】
Imports System.Windows.Forms
Imports System.ComponentModel
'' カスタム列ヘッダクラスを使用するカスタム列クラス
Public Class DataGridViewCustomCheckBoxHeaderColumn
Inherits DataGridViewCheckBoxColumn
Public Sub New()
MyBase.DefaultHeaderCellType = GetType(DataGridViewCustomCheckBoxHeaderCell)
'' ソート方向を示すグリフ表示をさせないためにソート不可(コード上からのソートのみ)に設定
Me.SortMode = DataGridViewColumnSortMode.NotSortable
End Sub
<EditorBrowsable(EditorBrowsableState.Never), Browsable(False), _
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
Public Shadows ReadOnly Property DefaultHeaderCellType() As Type
Get
Return GetType(DataGridViewCustomCheckBoxHeaderCell)
End Get
End Property
End Class
【DataGridViewCustomCheckBoxHeaderCell】
Imports System.Windows.Forms
Imports System.ComponentModel
'' カスタム列ヘッダセル
Public Class DataGridViewCustomCheckBoxHeaderCell
Inherits DataGridViewColumnHeaderCell
Private _checkState As Boolean
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 Object, _
ByVal formattedValue As Object, _
ByVal errorText As String, _
ByVal cellStyle As DataGridViewCellStyle, _
ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, _
ByVal paintParts As DataGridViewPaintParts)
'' 既存のヘッダーは背景や枠だけ描画すればいいので、テキストは空指定
MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, _
cellState, String.Empty, String.Empty, String.Empty, cellStyle, _
advancedBorderStyle, paintParts)
'' 文字描画の必要領域を求める
Dim drawTextSize As SizeF
drawTextSize = graphics.MeasureString(formattedValue, cellStyle.Font)
'' チェックボックス本体の描画位置を求める
Dim location As New Point
location.X = cellBounds.X + (cellBounds.Width - drawTextSize.Width) / 2 - 16
location.Y = cellBounds.Y + (cellBounds.Height - 12) / 2
'' チェック状態からCheckBoxStateを求める
Dim checkBoxState As VisualStyles.CheckBoxState
checkBoxState = IIf(Me._checkState, _
VisualStyles.CheckBoxState.CheckedNormal, VisualStyles.CheckBoxState.UncheckedNormal)
Console.WriteLine("Paint: checkBoxState=" & checkBoxState.ToString())
'' チェックボックスを描画
System.Windows.Forms.CheckBoxRenderer.DrawCheckBox( _
graphics, location, cellBounds, formattedValue, cellStyle.Font, False, checkBoxState)
End Sub
Protected Overrides Sub OnClick(ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
_checkState = _checkState Xor True
Console.WriteLine("_checkState=" & _checkState.ToString())
MyBase.OnClick(e)
Me.RaiseCheckBoxCheckedChanged(EventArgs.Empty)
Me.DataGridView.InvalidateCell(Me)
End Sub
Protected Overrides Sub OnDoubleClick(ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
_checkState = _checkState Xor True
MyBase.OnDoubleClick(e)
Me.RaiseCheckBoxCheckedChanged(EventArgs.Empty)
Me.DataGridView.InvalidateCell(Me)
End Sub
Private Sub RaiseCheckBoxCheckedChanged(ByVal e As EventArgs)
'' チェックボックスの状態(On/Off)が変化した時の処理は以下に記述
'' サンプル
Console.WriteLine(String.Format("チェックボックスが[{0}]になりました。", _
IIf(Me._checkState, "On", "Off")))
End Sub
Public Property Checked() As Boolean
Get
Return _checkState
End Get
Set(ByVal value As Boolean)
_checkState = value
End Set
End Property
End Class
【Form】
Private Sub DataGridView1_ColumnHeaderMouseClick(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs)
Handles DataGridView1.ColumnHeaderMouseClick
DataGridView1_Check(sender, e)
End Sub
Private Sub DataGridView1_ColumnHeaderMouseDoubleClick(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs)
Handles DataGridView1.ColumnHeaderMouseDoubleClick
DataGridView1_Check(sender, e)
End Sub
Private Sub DataGridView1_Check(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs)
If DataGridView1.Columns(e.ColumnIndex).Name = "Column1" Then
Dim cell As DataGridViewCustomCheckBoxHeaderCell = _
DirectCast(DataGridView1.Columns(e.ColumnIndex).HeaderCell, _
DataGridViewCustomCheckBoxHeaderCell)
Dim flag As Boolean = False
flag = cell.Checked
Dim dRow As DataGridViewRow
For Each dRow In DataGridView1.Rows
If dRow.Cells(0).IsInEditMode = True Or _
dRow.Cells(2).IsInEditMode = True Or _
dRow.Cells(3).IsInEditMode = True Then
DataGridView1.EndEdit()
End If
dRow.Cells(0).Value = flag
dRow.Cells(2).Value = flag
dRow.Cells(3).Value = flag
Next
End If
End Sub
|