views:

1448

answers:

2

I want to give the row headers and column headers a custom background from an image using the .net component DataGridView. Is it possible to even do this? And if so, how?

I use Visual Studio 2008, windows application, C#.

+2  A: 

Its possible to change the attributes of the datagridview rowheader. You'll need to either handle the CellPainting or the RowPostPaint event and manually draw the image in the row header cell.

 protected override void  OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
        {
             // Your code to insert image/content per cell goes here    
        }
QuBaR
I still can't figure out how this code can help me achieve this.I do get where trying to override the RowPostPaint event but I still don't know how to use it to insert an images for the background of the cells
Pieter888
Never mind I figured it out, thanks QuBaR!
Pieter888
For future googlers: The same trick can be used for cells to give your cells a gradient background (using `LinearGradientBrush`), only use OnRowPrePaint, and set the cells' default background color to *Transparent*
BlueRaja - Danny Pflughoeft
A: 

On way to do this is to put a cssClass name per header element in the RowDataBound event like this and assign your background image in the css.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            foreach (TableCell c in e.Row.Cells)
            {
                c.CssClass = "bgimage";
            }
        }
    }

css:

.bgimage{ background-image:url(images/arrow-red-large-down.gif);
Kernicus
And how am I able to define the css itself. should it be an external file?
Pieter888
The proper way would be to include a reference to an external style sheet, but you can include the style inline on the page.
Kernicus
This is for ASP.net, not winforms
BlueRaja - Danny Pflughoeft