giving the datagrid a background image

K

kwarnke

Does anyone know how to give the datagrid a background image? I'm interested
in this because when I set its Dock property to Fill, if the columns aren't
wide enough or there aren't enough rows, it's BackColor shows through. I'd
like to replace this with an image.

I tried setting the background image for the form and setting the grid's
BackColor to:

grid.BackColor = Color.FromArgb(0, grid.BackColor);

But the runtime balks and says the data grid doesn't support a transparent
back color.

I also tried using the backcolor of Web.Transparent. This does the job, but
I can't for the life of me get it to repaint correctly. If I start my app,
put another app in front of it, then go back to my app, the background looks
like the preivously displayed app (it just doesn't repaint the background
image). The same thing happens if I filter the data in the grid - it looks
all screwed up.

Any ideas? Please post to newsgroup.

Thanks!
- Kevin
 
Y

Ying-Shen Yu[MSFT]

Hi Kevin,

You may try painting the background of the datagrid in this way:
<code>
class MyGrid : DataGrid
{
Image img = Image.FromFile("you backgroung image file");
public MyGrid()
{
this.BackgroundColor = Color.Transparent;
//Turn on the resize redraw on datagrid to make the background redraw
correctly on resizing.
this.SetStyle(ControlStyles.ResizeRedraw,true);
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
pevent.Graphics.DrawImage(img,this.ClientRectangle);
base.OnPaintBackground (pevent);
}
}
</code>

Does it resolve your problem?
Feel free to reply this thread if you still have problem on this issue.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
K

kwarnke

This works but leads me to 2 additional issues:

1. This paints a stretched version of the image. Using the BackgroundImage
of the Form object it automatically tiled. This is something I can figure
out by just painting the image multiple times.

2. This causes a noticable flash as I'm filtering the datagrid. Any ideas
on how to avoid this?

Thanks!
- Kevin
 
Y

Ying-Shen Yu[MSFT]

Hi Kevin,

After investigating this issue further, I found the datagrid did all
painting work in the OnPaint method, so the OnPaint method was called
frequently when a change was made datagrid. So OnPaintBackground will also
be executed frequently, which caused flicker.

OK, we can't avoid the flicker in that way.
This way should work:
You may set the BackColor to Transparent , we still need derive from the
datagrid and override the CreateParams property to add the
WS_EX_TRANSPARENT flag, then set the background image in the parent control
of the datagrid.
Here is a sample snippet for this approach:
<code>
/// Set background image in the parent control
class TransparentDataGrid: DataGrid
{
public TransparentDataGrid()
{
this.BackgroundColor = Color.Transparent;
}

protected override CreateParams CreateParams
{
get
{
CreateParams cp=base.CreateParams;
cp.ExStyle|=0x00000020; //WS_EX_TRANSPARENT
return cp;
}
}
}
</code>
Does it work for you?
Feel free to reply this thread if you still have problem on this issue.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 

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