2 Replies - 552 Views - Last Post: 11 November 2017 - 01:29 PM Rate Topic: -----

#1 deanobravo   User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 142
  • Joined: 02-January 12

Datagridview Cellpaint Event Font Display Issue

Posted 10 November 2017 - 08:19 PM

I am using the datagridview CellPainting event to display a small coloured square in the upper right corner of the cell when the contents of the cell has a comment associated with it.
This works well except the text displayed in the commented cell looks like a draft version of the font that is being used.
The code is pretty much copied from msdn with a few minor tweaks.
It doesn't look that bad but being a bit of a perfectionist it bugs me.

 
Private Sub dgvWeeklyShift_cellpainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles dgvWeeklyShift.CellPainting

   Dim newRect As New Rectangle(e.CellBounds.X + e.CellBounds.Width - 8, e.CellBounds.Y, 6, 6)
   Dim backColorBrush As New SolidBrush(e.CellStyle.BackColor)
   Dim gridBrush As New SolidBrush(dgvWeeklyShift.GridColor)
   Dim gridLinePen As New Pen(gridBrush)
   Dim gridFont As New Font("Calibri", 9.75)

  Try

    ' Erase the 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)


    ' Draw the text content of the cell, ignoring alignment.
    If (e.Value IsNot Nothing) Then
       e.Graphics.DrawString(CStr(e.Value), gridFont, Brushes.Black, e.CellBounds.X + 1, e.CellBounds.Y + 2, 
       StringFormat.GenericDefault)
    End If

    ' Draw the inset highlight box.
    e.Graphics.DrawRectangle(Pens.Crimson, newRect)
    e.Graphics.FillRectangle(Brushes.Crimson, newRect)

    e.Handled = True

  Finally
    gridLinePen.Dispose()
    gridBrush.Dispose()
    backColorBrush.Dispose()
 End Try

End sub




I have a screencap showing the output to upload but can't get it to work. I'll keep trying.

This post has been edited by deanobravo: 10 November 2017 - 08:22 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Datagridview Cellpaint Event Font Display Issue

#2 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1499
  • View blog
  • Posts: 3,797
  • Joined: 01-February 13

Re: Datagridview Cellpaint Event Font Display Issue

Posted 11 November 2017 - 05:52 AM

You do not need to do all drawing if you just want to add a small rectangle at the top right of the cell. You can just use the Paint Method of the DataGridViewCellPaintingEventArgs Class that is passed to the e parameter of the CellPainting event sub to draw the complete cell as it normally would be. Then all you would need to draw is the rectangle in the corner.

For example...
    Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting

        'make sure the row and column index are both 0 or greater. The column headers and row headers have an index of -1 and you don't want to draw the rectangle in those.
        If e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then

            e.Paint(e.ClipBounds, DataGridViewPaintParts.All) 'draw all the parts of the cell normally.

            Dim newRect As New Rectangle(e.CellBounds.Right - 8, e.CellBounds.Y + 2, 6, 6)
            e.Graphics.FillRectangle(Brushes.Crimson, newRect) 'draw your filled rectangle

            e.Handled = True 'set the Handled property to true to let the DataGridView know that you handled painting this cell

        End If

    End Sub


Was This Post Helpful? 1
  • +
  • -

#3 deanobravo   User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 142
  • Joined: 02-January 12

Re: Datagridview Cellpaint Event Font Display Issue

Posted 11 November 2017 - 01:29 PM

That's brilliant, thanks man. :bigsmile:
When I think about how long I've been doing this and about how little I actually know, sad! <_<
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1