Click here to Skip to main content
15,881,715 members
Articles / Desktop Programming / Windows Forms
Article

Disabled Checkbox Column in the DataGridView

Rate me:
Please Sign up or sign in to vote.
4.67/5 (5 votes)
17 Dec 2008CPOL1 min read 96.4K   2.9K   66   3
This control allows the user to add column type other than existing column type for datagridview
HDisabledCheckBox

Introduction

While a cell can be read-only to prevent it from being editable, the DataGridView does not have built-in support for disabling a cell. Normally the concept of “disabled” means that the user cannot navigate to it and usually has a visual cue that it is disabled.  There isn't any easy way to create the navigational side of disabled, but the visual cue is something that can be done. While none of the built-in cells have a disabled property, the following example extends the DataGridViewCheckBoxCell and implements a visual “disabled” state along with a corresponding Enabled property.

Using the Code 

The DisabledCheckBox cell gives the user a visual impression that the checkbox cannot be edited. It is some sort of extension to the ReadOnly property for any cell in datagridview. But it gives visual cues to that. This DisabledCheckBox cell type is added as the Column type in the DataGridView like other Column type , textbox column, etc.

Create the supporting column type for the disabled checkbox cell which will be derived from DataGridViewCheckBoxColumn. Set the CellTemplate to be the DataGridViewDisableCheckBoxCell which we will write now.

I have added 7 rows on loading of form. The second checkbox is normal checkboxcolumn provided in the datagridview just for the comparison.

C#
public class DataGridViewDisableCheckBoxColumn : DataGridViewCheckBoxColumn
{
    public DataGridViewDisableCheckBoxColumn()
    {
        this.CellTemplate = new DataGridViewDisableCheckBoxCell();
    }
}

Now code the main cell type which will be derived from DataGridViewCheckBoxCell

The Enabled property decides whether the checkbox cell be checked or unchecked.

Override the Clone method to copy the Enabled property.

Now just paint the checkbox area with the checked/unchecked mark.

C#
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;
        } 
	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)
	{
            
            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);
            
                if (this.enabledValue)
                    CheckBoxRenderer.DrawCheckBox(graphics, drawInPoint, 
		    System.Windows.Forms.VisualStyles.CheckBoxState.CheckedDisabled);
                else
                    CheckBoxRenderer.DrawCheckBox(graphics, drawInPoint, 
		   System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedDisabled); 
	}
} 

History

  • 17th December, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHere's the updated to make it DPI enabled Pin
FAF31-May-16 5:08
FAF31-May-16 5:08 
GeneralThanks Pin
YZK30-Mar-11 0:08
YZK30-Mar-11 0:08 
GeneralDisabled Checkbox Column Pin
Sergeych77713-Oct-09 3:57
Sergeych77713-Oct-09 3:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.