[英]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. 我正在使用Windows窗体应用程序,需要在其中将背景图像设置为数据网格视图的数据行。 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. 例如,如果我的数据网格中有4行,我想将图像重复到行背景,而不是整个网格视图。 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 将图像添加到Visual Studio项目中的Resources
Then in your MyDataGridView
set (through Designer or by code) DefaultCellStyle.BackColor = Transparent
然后在MyDataGridView
设置(通过设计器或通过代码) DefaultCellStyle.BackColor = Transparent
And finally add event handler for .RowPrePaint
最后为.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... 如果您还需要设置DefaultCellStyle.SelectionBackColor = Transparent
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.