/// <summary> /// DataGridViewDisableCheckBoxColumn /// 自定义disablecheckbox列 实现禁用效果 /// </summary> public class DataGridViewDisableCheckBoxColumn : DataGridViewCheckBoxColumn { public DataGridViewDisableCheckBoxColumn() { this.CellTemplate = new DataGridViewDisableCheckBoxCell(); } } 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; } public DataGridViewDisableCheckBoxCell() { this.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 (!this.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); } CheckBoxState state = value != null && (bool)value ? CheckBoxState.CheckedDisabled : CheckBoxState.UncheckedDisabled; Size size = CheckBoxRenderer.GetGlyphSize(graphics, state); Point center = new Point(cellBounds.X, cellBounds.Y); center.X += (cellBounds.Width - size.Width) / 2; center.Y += (cellBounds.Height - size.Height) / 2; CheckBoxRenderer.DrawCheckBox(graphics, center, state); } else { base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); } } }
使用:
private void dgvView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex < 0 || e.ColumnIndex > colSelect.Index) { return; }
DataRow dr = (dgvView.Rows[e.RowIndex].DataBoundItem as DataRowView)?.Row;
if (dr != null)
{
string status= DataFun.ConvertToStringByDB(dr["status"]);
if (status!= 'Doing')
{
DataGridViewDisableCheckBoxCell checkCell = (DataGridViewDisableCheckBoxCell)dgvView.Rows[e.RowIndex].Cells[colSelect.Index];
if (checkCell == null) { return; }
checkCell.Enabled = false;
checkCell.ReadOnly = true;
}
}
}
/// <summary>/// DataGridViewDisableCheckBoxColumn /// 自定义disablecheckbox列 实现禁用效果/// </summary>/* Started by AICoder, pid:pe484e7c5fz4fdf14e3d08f8e0bfa38107705bcf */public class DataGridViewDisableCheckBoxColumn : DataGridViewCheckBoxColumn{ public DataGridViewDisableCheckBoxColumn() { this.CellTemplate = new DataGridViewDisableCheckBoxCell(); }}
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; }
public DataGridViewDisableCheckBoxCell() { this.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 (!this.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); }
CheckBoxState state = value != null && (bool)value ? CheckBoxState.CheckedDisabled : CheckBoxState.UncheckedDisabled; Size size = CheckBoxRenderer.GetGlyphSize(graphics, state); Point center = new Point(cellBounds.X, cellBounds.Y); center.X += (cellBounds.Width - size.Width) / 2; center.Y += (cellBounds.Height - size.Height) / 2;
CheckBoxRenderer.DrawCheckBox(graphics, center, state); } else { base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); } }} 标签:DataGridViewCheckBoxColumn,cellBounds,DataGridViewPaintParts,禁用,DataGridView,Dat From: https://www.cnblogs.com/Wilson6/p/18426282