Results 1 to 6 of 6

Thread: [RESOLVED] [2005] Cell Image Backgrounds

  1. #1

    Thread Starter
    Hyperactive Member neef's Avatar
    Join Date
    Dec 2001
    Location
    Boston
    Posts
    311

    [RESOLVED] [2005] Cell Image Backgrounds

    I made several images with transparent backgrounds for a column of datagridview cells, thinking I could easily set the background color of the cell to whatever I want depending on the alternating rows and highlighted row.

    It's simple code and it should work (this is in the event to fill the datagridview for alternating row colors; the image is in the last column):
    Code:
      
            Dim RowCount As Integer = dgvRoster.Rows.Count - 1
            Dim Cell As DataGridViewCell = dgvRoster.Rows(RowCount).Cells.Item(dgvRoster.Columns.Count - 1)
            If RowCount Mod 2 = 0 Then
                Cell.Style.BackColor = Color.AliceBlue
            Else : Cell.Style.BackColor = Color.Azure
            End If
    The results are ugly; the transparent parts of the images shine right through to my coding window. My code has no effect on the images whatsoever.

    I'm wondering if there's a better image format for transparentcies than the one I chose (.gif). Would jpegs or some other format yield better results or am I totally barking up the wrong tree with my approach?
    Last edited by neef; Dec 16th, 2007 at 08:35 AM.
    Intermediate Level Programmer Extraordinaire

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [2005] Cell Image Backgrounds

    you could make 2 new images, 1 with AliceBlue background + 1 with Azure background instead of transparent backgrounds.

    if you want to use transparent images, .gif or .png are your only options. .jpg + .bmp don't support transparent backgrounds.

  3. #3

    Thread Starter
    Hyperactive Member neef's Avatar
    Join Date
    Dec 2001
    Location
    Boston
    Posts
    311

    Re: [2005] Cell Image Backgrounds

    [QUOTE=.paul.]you could make 2 new images, 1 with AliceBlue background + 1 with Azure background instead of transparent backgrounds.
    QUOTE]

    That's what I had working before, but it got messy when I changed the colors of the rows for something seperate.

    I overided the CellPainting event and took care of business there. Here's the code for anyone else with the same problem. Just make sure to set the background color in the Load event (or somewhere else before the grid cells draws themselves)
    Code:
       If dgvRoster.Columns("ImageColumn").Index = e.ColumnIndex Then
    
                Dim backColorBrush As New SolidBrush(e.CellStyle.BackColor)
                Dim gridBrush As New SolidBrush(dgvRoster.GridColor)
                Dim gridLinePen As New Pen(gridBrush)
                'Erase Cell
                e.Graphics.FillRectangle(backColorBrush, e.CellBounds)
                ' Draw the grid lines (only the right and bottom lines;
                ' DataGridView takes care of the others).
                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, _
                    e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, _
                    e.CellBounds.Bottom - 1)
                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, _
                    e.CellBounds.Top, e.CellBounds.Right - 1, _
                    e.CellBounds.Bottom)
                gridLinePen.Dispose()
                gridBrush.Dispose()
                backColorBrush.Dispose()
    
            End If
    Code must be added to the rowenter and rowleave events as well if you want to include highlighting in addition to alternating rows.
    Intermediate Level Programmer Extraordinaire

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Cell Image Backgrounds

    Am I missing something or are you just trying to colour alternating rows different colours? If that is the aim then the DataGridView already has a property for this: AlternatingRowsDefaultCellStyle
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [2005] Cell Image Backgrounds

    you're missing something. the OP was having difficulty displaying transparent images in alternately coloured grid cells

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Cell Image Backgrounds

    Quote Originally Posted by .paul.
    you're missing something.
    It's been known to happen.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width