One way to achieve this is to simply override the Paint method from the DataGridViewButtonCell and call DrawImage from the graphics parameter. The call must be completed after the base call.
public class DeleteCell : DataGridViewButtonCell { Image del = Image.FromFile("..\\..\\img\\delete.ico"); 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) { base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); graphics.DrawImage(del, cellBounds); } }
After that, just create your own DataGridViewButtonColumn and configure the created DeleteCell as the cell template:
public class DeleteColumn : DataGridViewButtonColumn { public DeleteColumn() { this.CellTemplate = new DeleteCell(); this.Width = 20;
What is it. Now for the DataGridView, use DeleteColumn:
dgvBookings.Columns.Add(new DeleteColumn());
If the result of pressing the button depends on the row of the cell, be sure to process the clicks to the right, that is, to catch the index of the row of the cell.