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. Cell alignment in DataGridView [modified]

Cell alignment in DataGridView [modified]

Scheduled Pinned Locked Moved Visual Basic
cssgraphics
3 Posts 2 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

    Some columns on the grid need to be centred. I have set the columns to centred and this works OK, except that on the column where the cell is repainted a different colour based on the cell value, the centering is lost. I have set up some cell formats to change the cell alignment within the CellPainting event:- formatCell.Alignment = StringAlignment.Center formatCell.LineAlignment = StringAlignment.Center formatCell.FormatFlags = StringFormatFlags.NoClip Using the code below I get the value in the extreme top left of the cell instead of the centre. If nsCellValue = 0 Then 'This is a full outage and is shown Red ' Erase the cell. e.Graphics.FillRectangle(redBackBrush, 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 If Not (e.Value Is Nothing) Then e.Graphics.DrawString(CStr(nsCellValue), e.CellStyle.Font, _ Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 2, _ formatCell) End If e.Handled = True etc... The formatCell in the Drawstring property just does not seem to work. Any ideas on what I am doing wrong would be appreciated. :confused: -- modified at 7:40 Tuesday 16th January, 2007 David Loring !! Keep Music Live !! -- modified at 7:40 Tuesday 16th January, 2007

    D 1 Reply Last reply
    0
    • D DA_Loring

      Some columns on the grid need to be centred. I have set the columns to centred and this works OK, except that on the column where the cell is repainted a different colour based on the cell value, the centering is lost. I have set up some cell formats to change the cell alignment within the CellPainting event:- formatCell.Alignment = StringAlignment.Center formatCell.LineAlignment = StringAlignment.Center formatCell.FormatFlags = StringFormatFlags.NoClip Using the code below I get the value in the extreme top left of the cell instead of the centre. If nsCellValue = 0 Then 'This is a full outage and is shown Red ' Erase the cell. e.Graphics.FillRectangle(redBackBrush, 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 If Not (e.Value Is Nothing) Then e.Graphics.DrawString(CStr(nsCellValue), e.CellStyle.Font, _ Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 2, _ formatCell) End If e.Handled = True etc... The formatCell in the Drawstring property just does not seem to work. Any ideas on what I am doing wrong would be appreciated. :confused: -- modified at 7:40 Tuesday 16th January, 2007 David Loring !! Keep Music Live !! -- modified at 7:40 Tuesday 16th January, 2007

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Yeah, I've gotten some goofy results out of DrawString too. This is what I eventually found that worked for me.

      Dim sf As New StringFormat()
      sf.FormatFlags = StringFormatFlags.NoWrap Or StringFormatFlags.FitBlackBox
      sf.LineAlignment = StringAlignment.Center
      sf.Alignment = StringAlignment.Center
      sf.Trimming = StringTrimming.None

      Dim g As Graphics = e.Graphics
      g.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit
      g.DrawString( text, font, brush, cellwidth / 2, cellHeight / 2, sf)
      g.Dispose()

      Dave Kreskowiak Microsoft MVP - Visual Basic

      D 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Yeah, I've gotten some goofy results out of DrawString too. This is what I eventually found that worked for me.

        Dim sf As New StringFormat()
        sf.FormatFlags = StringFormatFlags.NoWrap Or StringFormatFlags.FitBlackBox
        sf.LineAlignment = StringAlignment.Center
        sf.Alignment = StringAlignment.Center
        sf.Trimming = StringTrimming.None

        Dim g As Graphics = e.Graphics
        g.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit
        g.DrawString( text, font, brush, cellwidth / 2, cellHeight / 2, sf)
        g.Dispose()

        Dave Kreskowiak Microsoft MVP - Visual Basic

        D Offline
        D Offline
        DA_Loring
        wrote on last edited by
        #3

        Thanks very much Dave. I'll give that a whirl!

        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