Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Colouring and Centering DataGridView Cells [modified]

Colouring and Centering DataGridView Cells [modified]

Scheduled Pinned Locked Moved Visual Basic
graphicscssquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DA_Loring
    wrote on last edited by
    #1

    HI All - I few days ago I posed a question about this and got an answer, but in the end this particular method did not work for me. I have managed to find a way to do it and I thought I would share it. Hope this helps someone. In the CellPainting event.......... Dim greenBackBrush As New SolidBrush(Color.LightGreen) Dim gridBrush As New SolidBrush(Me.dgvAvail.GridColor) Dim gridLinePen As New Pen(gridBrush) Dim nsCellValue As Single = 0 Dim nsMaxValue As Single = 0 Dim sf As New StringFormat sf.FormatFlags = StringFormatFlags.NoWrap Or StringFormatFlags.FitBlackBox sf.LineAlignment = StringAlignment.Center sf.Alignment = StringAlignment.Center sf.Trimming = StringTrimming.None 'This cell shown Green ' Erase the cell. e.Graphics.FillRectangle(greenBackBrush, 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) If Not (e.Value Is Nothing) Then Dim g As Graphics = e.Graphics Dim r As System.Drawing.Rectangle r.Width = (e.CellBounds.Right - 2) - (e.CellBounds.Left + 2) r.Height = (e.CellBounds.Bottom - 2) - (e.CellBounds.Top + 2) r.X = e.CellBounds.X + 2 r.Y = e.CellBounds.Y + 2 e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit e.Graphics.DrawString(CStr(nsCellValue), e.CellStyle.Font, Brushes.Black, r, sf) r = Nothing End If :) See the article for the full subroutine code -- modified at 13:41 Saturday 20th January, 2007 David Loring !! Keep Music Live !!

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups