public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataGridViewDisableButtonColumn disbtn = new DataGridViewDisableButtonColumn();
this.dataGridView1.Columns.Add(disbtn);
this.dataGridView1.Columns.Add(new DataGridViewTextBoxColumn());
this.dataGridView1.Rows.Add(5);
((DataGridViewDisableButtonCell)this.dataGridView1[0, 0]).Hide = true;
}
}
public class DataGridViewDisableButtonColumn : DataGridViewButtonColumn
{
public DataGridViewDisableButtonColumn()
{
this.CellTemplate = new DataGridViewDisableButtonCell();
}
}
public class DataGridViewDisableButtonCell : DataGridViewButtonCell
{
private bool hideValue;
public bool Hide
{
get
{
return hideValue;
}
set
{
hideValue = value;
}
}
// Override the Clone method so that the Hide property is copied.
public override object Clone()
{
DataGridViewDisableButtonCell cell =
(DataGridViewDisableButtonCell)base.Clone();
cell.Hide = this.Hide;
return cell;
}
// By default, display the button cell.
public DataGridViewDisableButtonCell()
{
this.hideValue = false;
}
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)
{
// The button cell is hide, so paint the border,
// background, and hide button for the cell.
if (this.hideValue)
{
// Draw the cell background, if specified.
if ((paintParts & DataGridViewPaintParts.Background) ==
DataGridViewPaintParts.Background)
{
SolidBrush cellBackground =
new SolidBrush(cellStyle.BackColor);
graphics.FillRectangle(cellBackground, cellBounds);
cellBackground.Dispose();
}
// Draw the cell borders, if specified.
if ((paintParts & DataGridViewPaintParts.Border) ==
DataGridViewPaintParts.Border)
{
PaintBorder(graphics, clipBounds, cellBounds, cellStyle,
advancedBorderStyle);
}
// if we don't want to draw the button at all, comment out the following lines.
// Calculate the area in which to draw the button.
//Rectangle buttonArea = cellBounds;
//Rectangle buttonAdjustment =
// this.BorderWidths(advancedBorderStyle);
//buttonArea.X += buttonAdjustment.X;
//buttonArea.Y += buttonAdjustment.Y;
//buttonArea.Height -= buttonAdjustment.Height;
//buttonArea.Width -= buttonAdjustment.Width;
// Draw the disabled button.
//ButtonRenderer.DrawButton(graphics, buttonArea,
// PushButtonState.Disabled);
// Draw the disabled button text.
//if (this.FormattedValue is String)
//{
// TextRenderer.DrawText(graphics,
// (string)this.FormattedValue,
// this.DataGridView.Font,
// buttonArea, SystemColors.GrayText);
//}
}
else
{
// The button cell isn't hide, so let the base class
// handle the painting.
base.Paint(graphics, clipBounds, cellBounds, rowIndex,
elementState, value, formattedValue, errorText,
cellStyle, advancedBorderStyle, paintParts);
}
}
}
If you have any other questions, please feel free to ask.
Best regards,
Ling
Please
remember to click “Mark as Answer” on the post that helps you, and to
click “Unmark as Answer” if a marked post does not actually answer your
question. This can be beneficial to other community members reading the
thread.