how to give DataGridViewButtonColumn a background image

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??
 
M

Morten Wennevik [C# MVP]

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;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top