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;
public bool Enabled
{
get
{
return enabledValue;
}
set
{
enabledValue = value;
}
}
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