user1826361
user1826361

Reputation: 21

Set DataGridView Row Background Image in C# Winform Application

I am working on a Windows form application where I need to set background image to the data row of data-grid view. For example if i am having 4 rows in my data grid, I want to repeat a image to the rows background but not to the whole grid-view. Image should be set as background image so that I can have text above the image. How could I achieve this? Please help. Thanks in advance.

Upvotes: 2

Views: 2288

Answers (2)

Fabio
Fabio

Reputation: 32445

Add image to Resources in your Visual Studio project

Then in your MyDataGridView set (through Designer or by code) DefaultCellStyle.BackColor = Transparent

And finally add event handler for .RowPrePaint

//In constructor
MyDataGridView.RowPrePaint += new EventHandler(MyDataGridView_RowPrePaint);

//Create event handler
private void MyDataGridView_RowPrePaint(Object sender, DataGridViewRowPrePaintEventArgs e)
{
    e.Graphics.DrawImage(My.Resources.MyImage, e.RowBounds);
}

If you need set DefaultCellStyle.SelectionBackColor = Transparent too...

Upvotes: 0

gwt
gwt

Reputation: 2413

you can do something like :

Image img;

public Form1()
{
    InitializeComponent();
    img = Image.FromFile(@"C:\Pictures\1.jpg");
}

private void GV_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawImageUnscaled(img, new Point(0, 0));
}

Upvotes: 1

Related Questions