Click here to Skip to main content
Click here to Skip to main content
Go to top

Disabled Checkbox Column in the DataGridView

, 17 Dec 2008
Rate this:
Please Sign up or sign in to vote.
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.

    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
        {
            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)

Share

About the Author

harschel

United States United States
No Biography provided

Comments and Discussions

 
GeneralThanks PingroupYZK30-Mar-11 0:08 
GeneralDisabled Checkbox Column PinmemberSergeych77713-Oct-09 3:57 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 17 Dec 2008
Article Copyright 2008 by harschel
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid