Home All Groups Group Topic Archive Search About

Using an image in a datagrid column header..

Author
13 Feb 2007 10:36 PM
modi321
I am looking to replace the text of a column header with an image.  Is
there any particular trick to do this?  The image-column only allows
for images in the actual rows, but I want to substitute a column's
text with an image.

Thanks for the help.

Oh, and I saw this from Feb 2004, and I am not sure if that reflects
the 2.0 framework or any current tricks in the last three years.

http://groups.google.com/group/microsoft.public.dotnet.framework.windowsforms/browse_thread/thread/53845e1dbd97e466/e160bae0395ccf9b?lnk=st&q=.net+grid+image+instead+of+header&rnum=1&hl=en#e160bae0395ccf9b

Author
13 Feb 2007 11:08 PM
ClayB
One way you can do this is to use the CellsPainting event to draw the
bitmap for a particular header cell. Here is code that does this
assuming the bitmap is in an imagelist.

//this.images is an ImageList with your bitmaps
void dataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
    if (e.ColumnIndex == 1 && e.RowIndex == -1)
    {
        e.PaintBackground(e.ClipBounds, false);

        Point pt = e.CellBounds.Location;// where you want the bitmap
in the cell
        int offset = (e.CellBounds.Width -
this.images.ImageSize.Width) / 2;
        pt.X += offset;
        pt.Y += 1;
        this.images.Draw(e.Graphics, pt, 0);
        e.Handled = true;
    }
}
==================
Clay Burch
Syncfusion, Inc.
Fix windows and pc errors, click for free system scan

Author
14 Feb 2007 2:26 PM
modi321
Clay,

Yeah, I was hoping to not this route, but I will investigate.

It seems like if I can drop an image in a cell, then it wouldn't be
too far off to drop an image in a column header.  Arg



Show quoteHide quote
On Feb 13, 5:08 pm, "ClayB" <c***@syncfusion.com> wrote:
> One way you can do this is to use the CellsPainting event to draw the
> bitmap for a particular header cell. Here is code that does this
> assuming the bitmap is in an imagelist.
>
> //this.images is an ImageList with your bitmaps
> void dataGridView1_CellPainting(object sender,
> DataGridViewCellPaintingEventArgs e)
> {
>     if (e.ColumnIndex == 1 && e.RowIndex == -1)
>     {
>         e.PaintBackground(e.ClipBounds, false);
>
>         Point pt = e.CellBounds.Location;// where you want the bitmap
> in the cell
>         int offset = (e.CellBounds.Width -
> this.images.ImageSize.Width) / 2;
>         pt.X += offset;
>         pt.Y += 1;
>         this.images.Draw(e.Graphics, pt, 0);
>         e.Handled = true;
>     }}
>
> ==================
> Clay Burch
> Syncfusion, Inc.
Author
14 Feb 2007 2:55 PM
modi321
Oh, and I know I can adapt this for the header, but I am not looking
to have it in the cell, but in the caption like this:

[URL=http://img234.imageshack.us/my.php?image=20070214gridka3.jpg]
[IMG]http://img234.imageshack.us/img234/9291/20070214gridka3.th.jpg[/
IMG][/URL]


Show quoteHide quote
On Feb 13, 5:08 pm, "ClayB" <c***@syncfusion.com> wrote:
> One way you can do this is to use the CellsPainting event to draw the
> bitmap for a particular header cell. Here is code that does this
> assuming the bitmap is in an imagelist.
>
> //this.images is an ImageList with your bitmaps
> void dataGridView1_CellPainting(object sender,
> DataGridViewCellPaintingEventArgs e)
> {
>     if (e.ColumnIndex == 1 && e.RowIndex == -1)
>     {
>         e.PaintBackground(e.ClipBounds, false);
>
>         Point pt = e.CellBounds.Location;// where you want the bitmap
> in the cell
>         int offset = (e.CellBounds.Width -
> this.images.ImageSize.Width) / 2;
>         pt.X += offset;
>         pt.Y += 1;
>         this.images.Draw(e.Graphics, pt, 0);
>         e.Handled = true;
>     }}
>
> ==================
> Clay Burch
> Syncfusion, Inc.
Author
15 Feb 2007 3:05 PM
Ciaran O''Donnell
The code provided does do it for the header.


Show quoteHide quote
"modi321" wrote:

> Oh, and I know I can adapt this for the header, but I am not looking
> to have it in the cell, but in the caption like this:
>
> [URL=http://img234.imageshack.us/my.php?image=20070214gridka3.jpg]
> [IMG]http://img234.imageshack.us/img234/9291/20070214gridka3.th.jpg[/
> IMG][/URL]
>
>
> On Feb 13, 5:08 pm, "ClayB" <c***@syncfusion.com> wrote:
> > One way you can do this is to use the CellsPainting event to draw the
> > bitmap for a particular header cell. Here is code that does this
> > assuming the bitmap is in an imagelist.
> >
> > //this.images is an ImageList with your bitmaps
> > void dataGridView1_CellPainting(object sender,
> > DataGridViewCellPaintingEventArgs e)
> > {
> >     if (e.ColumnIndex == 1 && e.RowIndex == -1)
> >     {
> >         e.PaintBackground(e.ClipBounds, false);
> >
> >         Point pt = e.CellBounds.Location;// where you want the bitmap
> > in the cell
> >         int offset = (e.CellBounds.Width -
> > this.images.ImageSize.Width) / 2;
> >         pt.X += offset;
> >         pt.Y += 1;
> >         this.images.Draw(e.Graphics, pt, 0);
> >         e.Handled = true;
> >     }}
> >
> > ==================
> > Clay Burch
> > Syncfusion, Inc.
>
>
>

Bookmark and Share