65.9K
CodeProject 正在变化。 阅读更多。
Home

DataGridView 中的禁用复选框列

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.67/5 (5投票s)

2008 年 12 月 17 日

CPOL

1分钟阅读

viewsIcon

98055

downloadIcon

2963

此控件允许用户为 datagridview 添加现有列类型以外的列类型

HDisabledCheckBox

引言

虽然单元格可以设置为只读以防止编辑,但 DataGridView 没有内置支持来禁用单元格。通常,“禁用”的概念意味着用户无法导航到该单元格,并且通常具有视觉提示表明其已禁用。  创建禁用的导航方面并不容易,但视觉提示是可以实现的。虽然没有内置单元格具有“禁用”属性,但以下示例扩展了 DataGridViewCheckBoxCell 并实现了一个视觉“禁用”状态以及相应的 Enabled 属性。

使用代码 

DisabledCheckBox 单元格给用户一种复选框无法编辑的视觉印象。它是 datagridview 中任何单元格的 ReadOnly 属性的一种扩展。但它提供了视觉提示。这种 DisabledCheckBox 单元格类型被添加为 DataGridView 中的 Column 类型,就像其他 Column 类型、textbox 列等一样。

创建支持禁用 checkbox 单元格的 column 类型,该类型将从 DataGridViewCheckBoxColumn 派生。将 CellTemplate 设置为我们现在要编写的 DataGridViewDisableCheckBoxCell

我在表单加载时添加了 7 行。第二个 checkboxdatagridview 中提供的普通 checkboxcolumn,仅用于比较。

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

现在编写主要单元格类型,该类型将从 DataGridViewCheckBoxCell 派生。

Enabled 属性决定 checkbox 单元格是否可以选中或取消选中。

重写 Clone 方法以复制 Enabled 属性。

现在只需绘制 checkbox 区域,显示选中/未选中的标记即可。

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); 
	}
} 

历史

  • 2008 年 12 月 17 日:初始发布
© . All rights reserved.