meiowei 发表于 2013-1-24 21:04:07

实现Datagridview里checkbox的enabled效果

我们都知道,Datagridview控件里的checkbox是没有enabled事件的,但实际应用中我们有时会需要实现这种效果。怎么办呢?思想很简单,继承控件。
vb.net:
Public Class DataGridViewDisableCheckBoxColumn    Inherits DataGridViewCheckBoxColumn    Sub New()      Me.CellTemplate = New DataGridViewDisableCheckBoxCell()    End SubEnd ClassPublic 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 SubEnd 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。
页: [1]
查看完整版本: 实现Datagridview里checkbox的enabled效果