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