Click here to Skip to main content
15,220,568 members

Disabled Checkbox Column in the DataGridView

Rate this:
4.67 (5 votes)
Please Sign up or sign in to vote.
4.67 (5 votes)
17 Dec 2008CPOL
This control allows the user to add column type other than existing column type for datagridview


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.

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.

public class DataGridViewDisableCheckBoxCell : DataGridViewCheckBoxCell
	private bool enabledValue;

        /// <summary>
        /// This property decides whether the checkbox should be shown 
        /// checked or unchecked.
        /// </summary>

        public bool Enabled
                return enabledValue;
                enabledValue = value;

        /// Override the Clone method so that the Enabled property is copied.

        public override object Clone()
            DataGridViewDisableCheckBoxCell cell =
            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);
            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, 
                    CheckBoxRenderer.DrawCheckBox(graphics, drawInPoint, 


  • 17th December, 2008: Initial post


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


About the Author

United States United States
No Biography provided

Comments and Discussions

QuestionHere's the updated to make it DPI enabled Pin
FAF31-May-16 5:08
MemberFAF31-May-16 5:08 
GeneralThanks Pin
YZK30-Mar-11 0:08
MemberYZK30-Mar-11 0:08 
GeneralDisabled Checkbox Column Pin
Sergeych77713-Oct-09 3:57
MemberSergeych77713-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.

Posted 17 Dec 2008

Tagged as


64 bookmarked