C#禁用 Windows 窗体 DataGridView 控件的按钮列DataGridViewButtonColumn中的按钮

来源:互联网 发布:刷课软件 编辑:程序博客网 时间:2024/05/24 07:11
public class DataGridViewDisableButtonColumn : DataGridViewButtonColumn    {        public DataGridViewDisableButtonColumn()        {            CellTemplate = new DataGridViewDisableButtonCell();        }        public override sealed DataGridViewCell CellTemplate        {            get { return base.CellTemplate; }            set { base.CellTemplate = value; }        }    }    public class DataGridViewDisableButtonCell : DataGridViewButtonCell    {        private bool _enabledValue;        public bool Enabled        {            get { return _enabledValue; }            set { _enabledValue = value; }        }        public override object Clone()        {            DataGridViewDisableButtonCell cell = (DataGridViewDisableButtonCell) base.Clone();            cell.Enabled = Enabled;            return cell;        }        public DataGridViewDisableButtonCell()        {            _enabledValue = true;        }        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)        {            if (!_enabledValue)            {                if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)                {                    SolidBrush cellBackground = new SolidBrush(cellStyle.BackColor);                    graphics.FillRectangle(cellBackground, cellBounds);                    cellBackground.Dispose();                }                if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border)                {                    PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);                }                Rectangle buttonArea = cellBounds;                Rectangle buttonAdjustment = BorderWidths(advancedBorderStyle);                buttonArea.X += buttonAdjustment.X;                buttonArea.Y += buttonAdjustment.Y;                buttonArea.Height -= buttonAdjustment.Height;                buttonArea.Width -= buttonAdjustment.Width;                ButtonRenderer.DrawButton(graphics, buttonArea,                                          System.Windows.Forms.VisualStyles.PushButtonState.Disabled);                if (FormattedValue is String)                {                    TextRenderer.DrawText(graphics,                                          (string) FormattedValue,                                          DataGridView.Font,                                          buttonArea, SystemColors.GrayText);                }            }            else            {                base.Paint(graphics, clipBounds, cellBounds, rowIndex,                           elementState, value, formattedValue, errorText,                           cellStyle, advancedBorderStyle, paintParts);            }        }    }
	
				
		
原创粉丝点击