how to give DataGridViewButtonColumn a background image

  • Thread starter Thread starter Lamis
  • Start date Start date
L

Lamis

Hi
this is my code
DataGridViewButtonColumn buttonupColumn =
new DataGridViewButtonColumn();
buttonupColumn.HeaderText = "";
buttonupColumn.Name = "btnUp";
buttonupColumn.Text = "Up";
I want to have a background image to my button instear of the .text ="Up"

Any idea how to do that??
 
Hi Lamis,

You can use the DataGridView.CellPainting event to add custom drawing to any
cell. Set the DataGridViewCellPaintingEventArgs.Handled property to prevent
the custom drawing to be erased.

This code will add a magenta rectangle on top of any column called "Button",
in this case this happens to be a DataGridViewButtonColumn.


protected override void OnLoad(EventArgs e)
{
DataGridViewButtonColumn bc = new DataGridViewButtonColumn();
bc.Name = "Button";
dataGridView1.Columns.Add(bc);
dataGridView1.CellPainting += new
DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
}

void dataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == -1 || e.RowIndex == -1)
return;

if (dataGridView1.Columns[e.ColumnIndex].Name == "Button")
{
e.Paint(e.CellBounds, DataGridViewPaintParts.All);
e.Graphics.FillRectangle(Brushes.Magenta, e.CellBounds.Left
+ 10, e.CellBounds.Top + 5, 10, 10);
e.Handled = true;
}
}
 
Back
Top