我们都知道,Datagridview控件里的checkbox是没有enabled事件的,但实际应用中我们有时会需要实现这种效果。怎么办呢?思想很简单,继承控件。
vb.net:
Public Class DataGridViewDisableCheckBoxColumn
Inherits DataGridViewCheckBoxColumn
Sub New()
Me.CellTemplate = New DataGridViewDisableCheckBoxCell()
End Sub
End Class
Public Class DataGridViewDisableCheckBoxCell
Inherits DataGridViewCheckBoxCell
Dim enabledValue As Boolean
Public Property Enabled() As Boolean
Get
Return enabledValue
End Get
Set(ByVal value As Boolean)
enabledValue = value
End Set
End Property
Public Overrides Function Clone() As Object
Dim cell As New DataGridViewDisableCheckBoxCell
cell = MyBase.Clone
Return cell
End Function
Protected Overrides Sub Paint(ByVal graphics As System.Drawing.Graphics, ByVal clipBounds As System.Drawing.Rectangle, ByVal cellBounds As System.Drawing.Rectangle, ByVal rowIndex As Integer, ByVal elementState As System.Windows.Forms.DataGridViewElementStates, ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal advancedBorderStyle As System.Windows.Forms.DataGridViewAdvancedBorderStyle, ByVal paintParts As System.Windows.Forms.DataGridViewPaintParts)
MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
If (Me.enabledValue) Then
Dim cellBackground As New SolidBrush(cellStyle.BackColor)
graphics.FillRectangle(cellBackground, cellBounds)
cellBackground.Dispose()
PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle)
Dim checkBoxArea As Rectangle = cellBounds
Dim buttonAdjustment As Rectangle = Me.BorderWidths(advancedBorderStyle)
checkBoxArea.X += buttonAdjustment.X
checkBoxArea.Y += buttonAdjustment.Y
checkBoxArea.Height -= buttonAdjustment.Height
checkBoxArea.Width -= buttonAdjustment.Width
Dim drawInPoint As New Point(cellBounds.X + cellBounds.Width / 2 - 7, cellBounds.Y + cellBounds.Height / 2 - 7)
CheckBoxRenderer.DrawCheckBox(graphics, drawInPoint, System.Windows.Forms.VisualStyles.CheckBoxState.CheckedDisabled)
End If
End Sub
End Class
c#:
public class DataGridViewDisableCheckBoxColumn : DataGridViewCheckBoxColumn
{
public DataGridViewDisableCheckBoxColumn()
{
this.CellTemplate = new DataGridViewDisableCheckBoxCell();
}
}
public class DataGridViewDisableCheckBoxCell : DataGridViewCheckBoxCell
{
private bool enabledValue;
/// <summary>
/// This property decides whether the checkbox should be shown checked or unchecked.
/// </summary>
public bool Enabled
{
get
{
return enabledValue;
}
set
{
enabledValue = value;
}
}
/// Override the Clone method so that the Enabled property is copied.
public override object Clone()
{
DataGridViewDisableCheckBoxCell cell =
(DataGridViewDisableCheckBoxCell)base.Clone();
cell.Enabled = this.Enabled;
return cell;
}
public DataGridViewDisableCheckBoxCell()
{
}
/// <summary>
/// Override the Paint method to show the disabled checked/unchecked datagridviewcheckboxcell.
/// </summary>
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds,
int rowIndex, DataGridViewElementStates elementState, object value,
object formattedValue, string errorText, DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
if (this.enabledValue)
{
SolidBrush cellBackground = new SolidBrush(cellStyle.BackColor);
graphics.FillRectangle(cellBackground, cellBounds);
cellBackground.Dispose();
PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
Rectangle checkBoxArea = cellBounds;
Rectangle buttonAdjustment = this.BorderWidths(advancedBorderStyle);
checkBoxArea.X += buttonAdjustment.X;
checkBoxArea.Y += buttonAdjustment.Y;
checkBoxArea.Height -= buttonAdjustment.Height;
checkBoxArea.Width -= buttonAdjustment.Width;
Point drawInPoint = new Point(cellBounds.X + cellBounds.Width / 2 - 7, cellBounds.Y + cellBounds.Height / 2 - 7);
CheckBoxRenderer.DrawCheckBox(graphics, drawInPoint, System.Windows.Forms.VisualStyles.CheckBoxState.CheckedDisabled);
}
}
}
使用方法很简单,将datagridview里的checkbox控件定义为DataGridViewDisableCheckBoxColumn型,将其enabled属性设置为true,就会看到效果。需要注意的地方是,虽然看起来控件是灰色,并且鼠标点击的时候checkbox选中状态不会改变,但是,该控件的value值还是改变了,如果你不想点击的时候改变其value值,那么再设置控件的Readonly=False。
分享到:
相关推荐
在窗体应用程序中,使用DataGridView中动态加入CheckBox列,并处理业务
C#_winform_DataGridView_checkbox复选框_实现单选效果
winform DataGridView表头带CheckBox全选;winform DataGridView表头带CheckBox全选
DataGridView表头添加checkbox实现全选反选
C#实现 DataGridViewCheckBoxColumn CheckBox全选
C#DataGridView中DataGridViewCheckBox问题
Datagridview控件没有全选或取消全选的复选框,本资源用VB2015通过扩展DataGridViewColumnHeaderCell类,在列头绘制一个复选框checkbox控件,通过定义checkbox鼠标单击事件来实现行的全选或取消全选。checkbox还可以...
Winfrom 开发表格DataGridView DataGridview 加入CheckBox
vb.net datagridview checkbox 选择数据及删除示例 核心代码: Try If MsgBox("您确定要删除选择的数据吗? ", MsgBoxStyle.Question + MsgBoxStyle.OkCancel, "温馨提示") = 1 Then Dim str As String = String....
高级控件(datagridview等)中加入checkbox实现全选和反选经典例子
重写DataGridView,在标头添加复选框,实现列中的复选框全选和反选
C#winform DataGridView checkbox复选框 实现单选效果,系统默认的是界面中多选,下午没事,做个单选的效果,同时复习下datagridview的常用知识,包含dantGridVIew的常用操作内容,注意事项,代码有点乱,学习。
在DataGridView添加CheckBox列,且只允许单选
CheckBox DataGridView fonts example
手动绘制winfrom DataGridView的列头CheckBox 实现单击列头的CheckBox全选和不全选!
解决方案用VS2017编程。在datagridview的列标题中添加checkbox,实现对整列进行全选,反选功能。
判断DatagridView里面的CheckBox是否选中,如果选中,启用另一控件。如果取消选中,则不启用。以及全选和反选的代码。
C# datagridview 中使用checkbox 全选、全消,以及窗体变大时控件位置随之变化C# datagridview 中使用checkbox 全选、全消,以及窗体变大时控件位置随之变化
DataGridView DataGridViewCheckBoxColumn编辑时实时触发事件代码
创建DataGridView checkbox 列