简体   繁体   中英

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.

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

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM