C#|C# WinForm DataGridView DataGridViewButtonColumn列禁用
先添加一下两个类
#region 禁用 DataGridViewButtonColumn
public class DataGridViewDisableButtonColumn : DataGridViewButtonColumn
{
public DataGridViewDisableButtonColumn()
{
this.CellTemplate = new DataGridViewDisableButtonCell();
}
}
public class DataGridViewDisableButtonCell : DataGridViewButtonCell
{
private bool enabledValue;
public bool Enabled
{
get
{
return enabledValue;
}
set
{
enabledValue = https://www.it610.com/article/value;
}
}
public override object Clone()
{
DataGridViewDisableButtonCell cell =
(DataGridViewDisableButtonCell)base.Clone();
cell.Enabled = this.Enabled;
return cell;
}
public DataGridViewDisableButtonCell()
{
this.enabledValue = https://www.it610.com/article/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);
}
Rectangle buttonArea = cellBounds;
Rectangle buttonAdjustment =
this.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 (this.FormattedValue is String)
{
TextRenderer.DrawText(graphics,
(string)this.FormattedValue,
this.DataGridView.Font,
buttonArea, SystemColors.GrayText);
}
}
else
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex,
elementState, value, formattedValue, errorText,
cellStyle, advancedBorderStyle, paintParts);
}
}
}
#endregion
【C#|C# WinForm DataGridView DataGridViewButtonColumn列禁用】
若DataGridView 已绑定数据使用以下方法
private void DisableButtonCell(DataGridView dgv,string strKey)
{
for (int j = 0;
j < dgv.RowCount;
j++)
{
if (dgv.Rows[j].Cells[strKey].Value.ToString() == "要禁用的数据值")
{
if (dgv.Rows[j].Cells[strKey] is DataGridViewDisableButtonCell)
((DataGridViewDisableButtonCell)dgv.Rows[j].Cells[strKey]).Enabled = false;
//禁用
}
}
}
转载于:https://www.cnblogs.com/hefy/p/7698526.html
推荐阅读
- winform|winform form之间传递参数
- WPF圆形进度条制作
- (转)c#中WinForm的TextBox循环自动滚动
- M_M|C# .Net WinForm 进度条ProgressBar 用法 实例
- C#|C# winform 使用进度条(两种形式)
- C#中的PropertyGrid控件在WinForm调试中的妙用
- C#dataGridView属性 表格设置
- c#用法技巧|c# winform 通过编程取消事件(event)的注册
- winform|C# winform progressbar +label显示进度
- WinForm控制progressBar 进度的一个简单方法