Vlastnost ReadOnly buňky s checkboxem sice znemožní její editaci, ale stav checkboxu bohužel zůstavá nezměněn. Zřejmě to jinak nepůjde, pro případnou potřebu přikládám kompletní příklad s vykreslením disabled checkboxu. Příspěvek lze označit jako vyřešené díky linku uvedenému níže: http://social.msdn.microsoft.com/Forums/... Code Snippet Imports System.Windows.Forms.VisualStyles
Public Class DataGridViewDisableCheckBoxColumn
Inherits DataGridViewCheckBoxColumn
Public Sub New()
MyBase.CellTemplate = New DataGridViewDisableCheckBoxCell
End Sub
End Class
Public Class DataGridViewDisableCheckBoxCell
Inherits DataGridViewCheckBoxCell
Private mEnabled As Boolean
Public Property Enabled() As Boolean
Get
Return mEnabled
End Get
Set(ByVal value As Boolean)
mEnabled = value
End Set
End Property
' Override the Clone method so that the Enabled property is copied.
Public Overrides Function Clone() As Object
Dim cell As DataGridViewDisableCheckBoxCell = DirectCast(MyBase.Clone(), DataGridViewDisableCheckBoxCell)
cell.Enabled = Me.Enabled
Return cell
End Function
' By default, enable the CheckBox cell.
Public Sub New()
Me.mEnabled = True
End Sub
Protected Overrides Sub Paint(ByVal graphics As Graphics, _
ByVal clipBounds As Rectangle, ByVal cellBounds As Rectangle, ByVal rowIndex As Integer, _
ByVal elementState 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)
Dim lCellBackground As Brush
Dim lCheckState As CheckState
Dim lCheckBoxState As CheckBoxState
' The checkBox cell is disabled, so paint the border,
' background, and disabled checkBox for the cell.
If Not mEnabled Then
' Draw the cell background, if specified.
If (paintParts And DataGridViewPaintParts.Background) = DataGridViewPaintParts.Background Then
If Me.Selected Then
lCellBackground = New SolidBrush(cellStyle.SelectionBackColor)
Else
lCellBackground = New SolidBrush(cellStyle.BackColor)
End If
graphics.FillRectangle(lCellBackground, cellBounds)
lCellBackground.Dispose()
End If
' Draw the cell borders, if specified.
If (paintParts And DataGridViewPaintParts.Border) = DataGridViewPaintParts.Border Then
PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle)
End If
lCheckState = CheckState.Unchecked
If (formattedValue IsNot Nothing) Then
If (formattedValue.GetType Is GetType(CheckState)) Then
lCheckState = DirectCast(formattedValue, CheckState)
ElseIf (formattedValue.GetType Is GetType(Boolean)) Then
If (DirectCast(formattedValue, Boolean)) Then lCheckState = CheckState.Checked
End If
End If
If lCheckState = CheckState.Checked Then
lCheckBoxState = CheckBoxState.CheckedDisabled
Else
lCheckBoxState = CheckBoxState.UncheckedDisabled
End If
' Calculate the area in which to draw the checkBox.
' force to unchecked!!
Dim size As Size = CheckBoxRenderer.GetGlyphSize(graphics, lCheckBoxState)
Dim center As Point = New Point(cellBounds.X, cellBounds.Y)
center.X += (cellBounds.Width - size.Width) / 2
center.Y += (cellBounds.Height - size.Height) / 2
' Draw the disabled checkBox.
' We prevent painting of the checkbox if the Width,
' plus a little padding, is too small.
If (size.Width + 4 < cellBounds.Width) Then
CheckBoxRenderer.DrawCheckBox(graphics, center, lCheckBoxState)
End If
Else
' The checkBox cell is enabled, so let the base class
' handle the painting.
MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, _
formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
End If
End Sub
End Class
|